1

I have a really simple backend with a couple routes. I want to keep the route logic outside the server.js file but for some reason when I do a POST request to the route it gives me a 404.

server.js

    // Call packages
var express   = require('express');
var bodyParser = require('body-parser');
var app       = express();
var morgan    = require('morgan');
var config    = require('./config.js');
var mongoose  = require('mongoose');
var path      = require('path');
var mandrill  = require('mandrill-api/mandrill');
var mandrill_client = new mandrill.Mandrill(config.mandrillApi);
var favicon = require('serve-favicon');

// Set up morgan to log HTTP requests
app.use(morgan('dev'));

// Set up mongo database
mongoose.connect(config.database);

// Parse body
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

// Server favicon
app.use(favicon(path.resolve('../client/content/images/logos/website-brand.png')));

//Routes
app.use('/sendMail', require(__dirname + '/routes/sendMail.js'));

// Default Routes
app.get('/', function (req, res) {
  res.sendfile(path.resolve('../client/views/index.html'));
});
app.get('/:name', function (req, res) {
  res.sendfile(path.resolve('../client/views/index.html'));
});


//Serve static files
app.use(express.static(path.resolve('../client')));
app.use('/scripts', express.static(path.resolve('../node_modules/angular-ui-bootstrap')));
app.use('/scripts', express.static(path.resolve('../node_modules/requirejs')));

//Log requests
app.use(function (req, res, next) {
  console.log(req.method, req.url);
  next();
});

//Start server
app.listen(config.port, function() {
  console.log('I\'m listening on port ' + config.port);
});

And sendMail.js

var config = require('../config.js');
var express = require('express');
var router = express.Router();
var mandrill = require('mandrill-api/mandrill');
var mandrill_client = new mandrill.Mandrill(config.mandrillApi);

// Routes
router.post(sendMail);

module.exports = router;

function sendMail(req, res, next) {
  console.log("Receiving in sendMail");

  var name = req.body.name;
  var email = req.body.email;
  var message = req.body.message;
  var toSend = {
    "html": "<p>"+message+"</p>",
    "text": message,
    "subject": "From Website",
    "from_email": email,
    "from_name": name,
    "to": [{
      "email": "",
      "name": "",
      "type": "to"
    }]
  };
  mandrill_client.messages.send({"message": toSend}, function(result) {
    console.log(result);
    var status = result[0].status;
    if (status == 'sent') {
      res.send({success: true});
    } else {
      res.send({success: false, reason: status});
    };
  }, function(e) {
    console.log("Mandrill Error: "+e.message);
    res.send({success: false, error: e});
  });
  next();
};

But when I do a POST request to /sendMail it gives me a 404

enter image description here

  • 1
    As best I can tell from the Express doc, `router.post()` requires a path as the first argument, not a function. – jfriend00 Jan 30 '16 at 10:17

1 Answers1

1

In sendMail.js remove at the end of file :

next();

because in this route your responses are in async callback, and before they are invoke, node.js get next() and therefore it move to next router. Because there is no more accurate routes it return 404.

One offtopic in server.js: Move your log request before sendMail router.

Krzysztof Sztompka
  • 7,066
  • 4
  • 33
  • 48
  • Neither of those things worked. I tried removing next (keeping as a parameter), and tried removing the parameter, and moved the route to below the request logger. Still getting 404 – Alex Goddijn Feb 01 '16 at 21:05