Node.js express POST 404ing

node js post request 404 error
express return 404
express 404
express route not found
express js url params
node js express form post example
post is not defined node js
post request not working node js

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.

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

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

Node.js server code:
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:

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

    }

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

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.

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

What you wanna do should look like this in practice: // routes/index.js module.​exports = (express) => { // Create express Router var router  Handling POST Requests in Express. In order to handle POST requests in Express JS, there are certain changes that we will need to make. These are: We will need to download and install body-parser. This is a middle-ware layer which will help us handle POST requests in Node.js using Express framework.

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")

This concept is left up to third-party Node modules, allowing you to interface with In Express, 404 responses are not the result of an error, so the error-handler  Node.js express POST 404ing Tengo una pequeña aplicación node.js que usa el marco express , pero por alguna razón no puedo hacer que mi aplicación responda a las requestes POST. En el logging del server simplemente obtengo "POST / 404 5 ms", y no puedo entender por qué.

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

// 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.

I'm using express 4.12.2 and when i set (where router was created previously var app = express(); app.use(router); app.listen(3000); $ node test.js & [1] 7140 When the form in index.jade is submitted, it is 404 not found. Introduction In this brief article we'll be going over how to extract information from a POST body in Express.js. The HTTP protocol provides a number of ways to pass information from a client to a server, with POST bodies being the most flexible and most commonly used method to send data via HTTP. Another way, which is typically used for different use-cases, is to convey information using

var express = require('express'); var kraken = require('kraken-js'); var options, app​; /* * Create and configure application. Also exports  node.js documentation: POST api using Express. Example. Following example create POST api using Express.This example is similar to GET example except the use of body-parser that parses the post data and add it to req.body.

I have programmed the following route to accept POST requests, but AJAX calls to this route always gives me the error 404: Not found. // integracoes.js var  Express Parsing Array from POST. Ask Question Asked 2 years, 8 months ago. Active 1 year, 6 months ago. node.js and express : how to wait for udp response. 1.

Now in the program im using express js not the natvie node js api. i use app.​post the server responds with a 404 can not get the request but if  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. I am trying to send data from my html for node.js

Comments
  • well, this isn't the answer to my problem, seeing as I am already using app.post(), and it isn't working. That's what my issue is.
  • You're using app.post at "/test" not "/". Read my answer more thoroughly.
  • And i'm saying that i was Posting /test, not /. I got it to work, this wasn't the issue. Manged to get it working last night, and now I can't remember which change I made did it. I seem to have a problem committing and labeling fixes properly when I'm tired...
  • ah. makes sense. Anyways, thanks for the help. Yours wasn't the correct fix for the problem, but close enough that I'll mark yours as the answer.