Nodemon - "clean exit - waiting for changes before restart" during setup

cosmicluna picture cosmicluna · Jan 31, 2018 · Viewed 36.2k times · Source

I am trying to set up a RESTful API with Node and Postgres. I have run into a problem where whenever I attempt to run the server (using npm start) to test it locally, I get the following output:

[nodemon] 1.14.10 [nodemon] to restart at any time, enter rs [nodemon] watching: . [nodemon] starting node index.js server.js [nodemon] clean exit - waiting for changes before restart

After searching online for quite some time, I cannot find too many resources on what exactly "clean exit - waiting for changes before restart" exactly means, especially in this case.

This is my queries.js file:

  1 var promise = require('bluebird');
  2 
  3 var options = {
  4   // Initialization Options
  5   promiseLib: promise
  6 };
  7 
  8 // created an instance of pg-promise, override default pgp lib w bluebird
  9 var pgp = require('pg-promise')(options);
 10 var connectionString = 'postgres://localhost:3000/actions';
 11 var db = pgp(connectionString);
 12 
 13 // add query functions
 14 
 15 module.exports = {
 16   getAllActions: getAllActions,
 17 //  getSingleAction: getSingleAction,
 18 //  createAction: createAction,
 19 //  updateAction: updateAction,
 20 //  removeAction: removeAction
 21 };
 22 
 23 function getAllActions(req, res, next) {
 24   db.any('select * from acts')
 25     .then(function (data) {
 26       res.status(200)
 27         .json({
 28           status: 'success',
 29           data: data,
 30           message: 'Retrieved ALL actions'
 31         });
 32     })
 33     .catch(function (err) {
 34       return next(err);
 35     });
 36 }

Here is my index.js file:

  3 var express = require('express');
  4 var app = express();
  5 var router = express.Router();
  6 var db = require('./queries');
  7 
  8 // structure: expressInstance.httpRequestMethod(PATH, HANDLER)
  9 app.get('/api/actions', db.getAllActions);
 10 //app.get('/api/actions/:id', db.getSingleAction);
 11 //app.post('/api/actions', db.createAction);
 12 //app.put('/api/actions/:id', db.updateAction);
 13 //app.delete('/api/actions/:id', db.removeAction);
 14 
 15 module.exports = router;

Any thoughts on what could be going on here? Thanks in advance.

Answer

Kamil Kulach picture Kamil Kulach · Jan 31, 2018

There are a few things wrong with your code. You never told your app to run. When you're ready creating your routes you should start your server with:

app.listen(<port on which the server should run here>);

Also you have an Express app and a router in the same file. The router should only be used as a sub module (handy when you want to divide your app over multiple files). Right now you're doing nothing with it. If you remove the router and the export then it should work fine.