title: Router returns 404 in Express.js
body: <p>I'm currently learning and a newbie to <code>Node.js</code>. I'm trying to build a simple <code>REST API</code> and currently getting an <code>error 404</code> when try post to a particular route in postman to test if data has successfully been sent to <code>Mongo db</code>. I'm sure what i'm missing. I have double checked all my routes and they seem fine. It works when i make a get request and falls through when i make a post request. </p>
  
  <p>This is my app.js </p>
  
  <pre><code>var createError = require('http-errors');
  var express = require('express');
  var path = require('path');
  var cookieParser = require('cookie-parser');
  var bodyParser = require('body-parser');
  var mongoose = require('mongoose');
  var logger = require('morgan');
  var indexRouter = require('./routes/index');
  var usersRouter = require('./routes/users');
  var Developer = require('./models/developers');
  var app = express();
  
  // view engine setup
  app.set('views', path.join(__dirname, 'views'));
  app.set('view engine', 'pug');
  app.use(logger('dev'));
  app.use(express.json());
  app.use(express.urlencoded({ extended: false }));
  app.use(cookieParser());
  app.use(bodyParser.urlencoded({ extended: true}));
  app.use(express.static(path.join(__dirname, 'public')));
  app.use(bodyParser.json());
  app.use('/', indexRouter);
  app.use('/users', usersRouter);
  
  // Connect to DB
  mongoose.connect('mongodb://127.0.0.1:27017');
  
  // API Routes
  var router = express.Router();
  
  // Routes will be prefixed with /api
  app.use('/api', router);
  
  // catch 404 and forward to error handler
  app.use(function(req, res, next) {
    next(createError(404));
  });
  
  // error handler
  app.use(function(err, req, res, next) {
    // set locals, only providing error in development
    res.locals.message = err.message;
    res.locals.error = req.app.get('env') === 'development' ? err : {};
  
    // render the error page
    res.status(err.status || 500);
    res.render('error');
  });
  
  module.exports = app;
  
  // Test Route
  router.get('/', function (req, res) {
    res.json({message: 'Welcome to my simple API!'});
  });
  
  router.route('/developers')
      .post(function (req, res) {
        var developer = new Developer(); // New instance of sa developer
        developer.firstName = req.body.firstName;
        developer.lastName = req.body.lastName;
        developer.jobTitle = req.body.jobTitle;
  
        developer.save(function (err) {
          if (err) {
            res.send(err);
          } else {
            res.json('Developer was successfully fetched');
          }
        });
      })
  
      .get(function (req, res) {
        Developer.find(function (err, developers) {
          if (err) {
            res.send(err);
          } else
            res.json(developers);
        });
      });
  
  router.route('/developer/:developer_id')
      .get(function (req, res) {
        Developer.findById(res.params.developer_id, function (err, developer) {
          if (err) {
            res.send(err);
          }
          res.json(developer);
        });
      });
  
  router.route('/developer/firstName/:firstName')
      .get(function (req, res) {
        Developer.find({firstName:res.params.firstName}, function (err, developer) {
          if (err) {
            res.send(err);
          }
          res.json(developer);
        });
      });
  </code></pre>
  
  <p>My model is developer.js </p>
  
  <pre><code>var mongoose = require('mongoose');
  var Schema = mongoose.Schema;
  
  var DevelopersSchema = new Schema({
     firstName: String,
     lastName: String,
     jobTitle: String
  });
  
  module.exports = mongoose.model('Developers', DevelopersSchema);
  </code></pre>