简体   繁体   English

Node.js和Express; 开机自检404

[英]Node.js and Express; POST 404

I can't get my server to call a function from the client using node.js and express. 我无法让服务器使用node.js和express从客户端调用函数。 I don't need to pass data, I just need to alert to the server that it should call a function. 我不需要传递数据,我只需要向服务器发出警报,它应该调用一个函数。 This is what I have (after following numerous tutorials): 这就是我所拥有的(在完成了许多教程之后):

client: 客户:

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

server: 服务器:

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

Error: 错误:

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

You have your middleware in the wrong order. 您的中间件顺序错误。 Express prossess them in order declared. Express按照声明的顺序处理它们。 Move your app.post('/admin... line ABOVE your Not Found middleware. 将您的app.post('/admin...行移至未找到的中间件app.post('/admin...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM