Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodemon keeps restarting server

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}`);
    });
});
like image 832
Dileet Avatar asked Jun 30 '17 23:06

Dileet


People also ask

Does Nodemon restart on crash?

Nodemon monitors your source files for changes during development and reload on change. That's why it says 'waiting for file change' after a crash. In development, usually you made a mistake, correct it, save the source file, nodemon notices that and reloads.

How does Nodemon restart?

Running non-Node code While Nodemon is running, we can manually restart our application. So instead of stopping and restarting Nodemon, we can just type rs and press enter, and Nodemon will restart the server or the running process for us.


2 Answers

From the documentation:

nodemon will watch the files in the directory in which nodemon was started, and if any files change, nodemon will automatically restart your node application.

If your db's .JSON file is under the watch of nodemon, and you're constantly writing to it, your server will restart in an infinite loop thus making it inaccessible. Try moving your .JSON file outside the scope of nodemon's watch via moving it outside your directory or via some nodemon configuration (if possible).

like image 169
Andrew Sinner Avatar answered Sep 27 '22 20:09

Andrew Sinner


I solved this issue from this page.

practically you just have to do

 nodemon --ignore 'logs/*'
like image 39
Yorsh Avatar answered Sep 27 '22 21:09

Yorsh