javascript Node.js 表达 POST 404ing

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16928829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 06:29:20  来源:igfitidea点击:

Node.js express POST 404ing

javascriptpostbackbone.jsexpresshttp-status-code-404

提问by LucienK

I've got a small node.js application using the express framework, but for some reason I can't get my application to respond to POST requests. In the server log I simply get "POST / 404 5ms", and I can't figure out why.

我有一个使用 express 框架的小型 node.js 应用程序,但由于某种原因,我无法让我的应用程序响应 POST 请求。在服务器日志中,我只是得到“POST / 404 5ms”,但我不知道为什么。

EDIT: To clarify - My problem is that app.post doesn't seem to be doing anything

编辑:澄清 - 我的问题是 app.post 似乎没有做任何事情

EDIT 2: I somehow managed to fix this last night, but now I can't figure out at what point i fixed it.

编辑 2:我昨晚以某种方式设法解决了这个问题,但现在我不知道我在什么时候修复了它。

Node.js server code:

Node.js 服务器代码:

var express = require('express')
  , routes = require('./routes')
  , user = require('./routes/user')
  , http = require('http')
  , path = require('path');

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('chocolatechip'));
app.use(express.session());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));


// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

//pages
//Landing page
app.get('/', routes.index);
app.post('/test',function(req,res){
  console.log(req.body);
  res.send("received post");
});
//return list containing users
//app.post('/users', user.list);
//return requested user
app.get('/users/:id', user.get);
//app.post('/users/login', user.login);


//server
http.createServer(app).listen(app.get('port'), function(){
  console.log('Server listening on port ' + app.get('port'));
});

On the actual webpage, I've got the following javascript code:

在实际网页上,我有以下 javascript 代码:

var login = $('#login');
  var page = $('#page');
  var register = $('#register');
  var userField = login.find('.user');
  var passField = login.find('.pass');
  var confPassField = login.find('.confpass');
  var form = $('.logform');
  $('#formbutton').on('click',function(){
    if(register.hasClass('hidden')){
      login.addClass('hidden');
      confPassField.val('');
      var logDat = JSON.stringify(form.serializeArray);
      userField.val('');
      passField.val('');
      page.html("Login form submitted");
      $.post(
        form.attr("action"),
        {test:"test"},
        function(data){
          alert("Response: "+data)
        }
      );

    }

回答by thomallen

If you are posting to / as your log is saying that you are "POST / 404 5ms", you need to change the following line:

如果您发布到 / 因为您的日志说您是“POST / 404 5ms”,则需要更改以下行:

app.get('/', routes.index);

to

app.all('/', routes.index);

This will allow a GET or POST to that route. You can also just use app.post()if you are only posting to that route. Hope this helps.

这将允许对该路由进行 GET 或 POST。app.post()如果您只发布到该路线,您也可以使用。希望这可以帮助。

Docs here: http://expressjs.com/api.html#app.all

文档在这里:http: //expressjs.com/api.html#app.all

回答by Xerri

Make sure that 'form.attr("action")' is getting the proper URL. It seems that your form is posting to the index page rather than to '/test'. Maybe that should be changed to $('form').attr("action")

确保 'form.attr("action")' 获得正确的 URL。您的表单似乎发布到索引页面而不是“/test”。也许应该改为$('form').attr("action")

回答by frankenapps

For me the problem was that I had my

对我来说,问题是我有我的

app.post('/test', jsonParser, function (req, res) {
    console.log(req);
    res.send('Ok');
});

below this part added by express-generator to my app.js

下面这部分由 express-generator 添加到我的 app.js

// catch 404 and forward to error handler
app.use(function(req, res, next) {
    next(createError(404));
});

By changing the order in the file I resolved this problem.

通过更改文件中的顺序,我解决了这个问题。