Nodemon keeps restarting server

Viewed 17694

I have an express server that uses a local json file for a database. I'm using https://github.com/typicode/lowdb for getters and setters.

Currently the server keeps starting and restarting without any problems, but can't access it. Below is my Server.js file:

import express from 'express'
import session from 'express-session'
import bodyParser from 'body-parser'
import promisify from 'es6-promisify'
import cors from 'cors'
import low from 'lowdb'
import fileAsync from 'lowdb/lib/storages/file-async'

import defaultdb from './models/Pages'

import routes from './routes/index.js'

const app = express();

const db = low('./core/db/index.json', { storage: fileAsync })

app.use(cors())

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.use('/', routes);

app.set('port', process.env.PORT || 1337);

db.defaults(defaultdb).write().then(() => {
    const server = app.listen(app.get('port'), () => {
      console.log(`Express running → PORT ${server.address().port}`);
    });
});

Anyone have an issue like this before? I think it has something to do with this line:

db.defaults(defaultdb).write().then(() => {
    const server = app.listen(app.get('port'), () => {
      console.log(`Express running → PORT ${server.address().port}`);
    });
});
8 Answers

I solved this issue from this page.

practically you just have to do

 nodemon --ignore 'logs/*'

My solution: I've added nodemonConfig in package.json file in order to stop infinite loop/restarting. In package.json:

"nodemonConfig": { "ext": "js", "ignore": ["*.test.ts", "db/*"], "delay": "2" },
"scripts": { "start": "nodemon" }

In my case (which is the same as the OP) just ignoring the database file worked

nodemon --ignore server/db.json server/server.js

You can use this generalized config file. Name it nodemon.json and put in the root folder of your project.

{
  "restartable": "rs",
  "ignore": [".git", "node_modules/", "dist/", "coverage/"],
  "watch": ["src/"],
  "execMap": {
    "ts": "node -r ts-node/register"
  },
  "env": {
    "NODE_ENV": "development"
  },
  "ext": "js,json,ts"
}

I solved this by creating a script in my package.json like this:

scripts": {
    "start-continuous": "supervisor server/server.js",
},

This will work if you have supervisor installed in your global scope.

npm install supervisor -g

Now all I do is: npm run start-continuous

I solved this by adding the following code to the package.json file

  "nodemonConfig": {
    "ext": "js",
    "ignore": [
      "*.test.ts",
      "db/*"
    ],
    "delay": "2"
  }
}
Related