app.use not working express 4

时间:2016-01-30 10:06:33

标签: javascript node.js express router

我有一个非常简单的后端,有几条路线。我想将路由逻辑保留在server.js文件之外,但出于某种原因,当我对路由发出POST请求时,它会给我一个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);
});

和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();
};

但是当我向/ sendMail发出POST请求时,它给了我一个404

enter image description here

1 个答案:

答案 0 :(得分:1)

在sendMail.js中删除文件末尾:

next();

因为在此路由中您的响应是异步回调,并且在它们被调用之前,node.js得到next(),因此它将移动到下一个路由器。由于没有更准确的路线,因此返回404。

server.js中的一个offtopic: 在sendMail路由器之前移动您的日志请求。