Node.js和Express; 开机自检404

我无法让我的服务器使用node.js和express来从客户端调用一个函数。 我不需要传递数据,我只需要警告服务器它应该调用一个函数。 这是我有(以下大量的教程):

客户:

$.ajax({ type: 'POST', url: 'http://localhost:3001/admin', sucess: function() { console.log('sucess'); } 

服务器:

 var express = require('express'); var app = express(); var server = require('http').Server(app); app.set('port', process.env.PORT || 3001); // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'hjs'); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); app.use('/', routes); // catch 404 and forward to error handler app.use(function(req, res, next) { var err = new Error('Not Found'); err.status = 404; next(err); }); app.post('/admin', function(req, res) { console.log("admin refresh"); res.send(200); }); 

错误:

 POST http://localhost:3001/admin 404 (Not Found) 

你有错误的顺序你的中间件。 Express按照顺序进行宣传。 移动你的app.post('/admin...在你的Not Found中间件上面。