Search code examples
apiexpressnpmpg-promise

npm restful api suddenly no longer working


I followed this tutorial to create a restful api with npm and postgres

Designing a RESTful API With Node and Postgres

I got everything to work without a problem, closed the server and went to other things.. when I got back, the routing stopped working suddenly giving 404 error.. I checked everything related to routing and I can't find the problem!

When I connect to localhost:3000 I get the correct express homepage but when I try to access the api, localhost:3000/api/patients the 404 error page appears

Here is my code

index.js

var express = require('express');
var router = express.Router();
var db = require('../queries');


router.get('/api/patients', db.getAllPatients);


module.exports = router;

queries.js

var promise = require('bluebird');

var options = {
  // Initialization Options
  promiseLib: promise
};

var pgp = require('pg-promise')(options);
var connectionString = 'postgres://localhost:5432/maindb'
var db = pgp(connectionString);

module.exports = {
  getAllPatients: getAllPatients
}

function getAllPatients(req, res, next) {
  db.any('select * from patients where deleted = false')
    .then(function (data) {
      res.status(200)
        .json({
          status: 'success',
          data: data,
          message: 'Retrieved ALL patients'
        });
    })
    .catch(function (err) {
      return next(err);
    });
}

Solution

  • It seems something was wrong with express installation, something corrupted it.

    I recreated the project from point zero and specified which version of express and bluebird to install, and everything seems to work fine without any problems.