javascript - app.use 不工作 Express 4

标签 javascript node.js express router

我有一个非常简单的后端,有几条路线。我想将路由逻辑保留在 server.js 文件之外,但由于某种原因,当我对路由发出 POST 请求时,它给了我一个 404。

服务器.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

最佳答案

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

next();

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

server.js 中的一个题外话: 将您的日志请求移至 sendMail 路由器之前。

关于javascript - app.use 不工作 Express 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35100412/

相关文章:

node.js - Express 中的 app.set 和 app.engine

node.js - app.use 中触发函数的顺序

javascript - Node.js 无法识别 Ajax 请求

javascript - 自动提交不适用于此脚本

javascript - 具有单个系列、可变条形颜色和图例的 Jqplot

javascript - 多次切片 JavaScript TypedArray

javascript - 将文档添加到父文件夹?

node.js - 如何从快速路线内发送到socket.io房间

javascript - 如何在 mongodb 中查询对象数组

javascript - Drive Realtime API XHR 错误处理程序崩溃