By Nicky Vai


2020-02-14 08:49:38 8 Comments

I have got a problem with Express, I am trying to use the app.post() function but it's not working and I don't know why...

Even though I have included the bodyParser()...

It returns 404 page not found error

var express = require('express');

var MongoClient = require('mongodb').MongoClient;

var router = express.Router();

router.post("/hello",(req, res) => {
        res.send('POST CALL');
    });

/* GET home page. */
router.get('/', function(req, res, next) {
    MongoClient.connect('mongodb://localhost:27017/nicky', function (err, client) {
        if (err) throw err

        var db = client.db('nicky')

        db.collection('student').find().toArray(function (err, result) {
            if (err) throw err
            res.send(JSON.stringify(result));
        })
    })
});
module.exports = router;

GET is working properly, but POST is not.

3 comments

@Zeeshan Hassan Memon 2020-02-14 11:43:04

I am not sure how rest of your code looks, but I have copied your provided snippet and it works this way:

express-post.js:

const express = require('express');
const router = express.Router();

// curl -X POST http://localhost:3000/bar/hello
router.post("/hello",(req, res) => {
    res.send('It is POST');
});

// curl -X GET http://localhost:3000/bar/hi
router.get('/hi', function(req, res, next) {
    res.send('It is GET');
});

module.exports = router;

express-post-server.js:

const express = require('express');
const bar = require('./express-post');
const app = express();

// curl -X GET http://localhost:3000/foo
app.get('/foo', function (req, res, next) {
    res.send('This is foo GET!');
});

// register
app.use('/bar', bar);

app.listen(3000);

For complete running example clone node-cheat and run node express-post.

@Nicky Vai 2020-02-15 07:49:04

GET / 404 280.007 ms - 1112 GET /stylesheets/style.css 304 6.553 ms - - GET /hi 200 1.588 ms - 9 GET /hello 404 19.140 ms - 1112 GET /stylesheets/style.css 304 0.564 ms - not work

@Zeeshan Hassan Memon 2020-02-15 08:48:52

/hello is a POST not GET, so it is giving a valid error, 404 means not found. You can use postman or curl as I have provided instructions in code snippet above.

@Nicky Vai 2020-02-16 14:37:31

ok, thank you it's work

@Manish Yadav 2020-02-14 09:21:26

May be you forgot to use app.use('/',require('import route here')); in main app.

@Chiranjeev 2020-02-14 09:08:21

Maybe you haven't required and initialized body-parser! Just confirm once if you have included this:

  const bodyParser = require('body-parser');
  // support parsing of application/json type post data
  app.use(bodyParser.json());

  //support parsing of application/x-www-form-urlencoded post data
  app.use(bodyParser.urlencoded({ extended: true }));

@Nicky Vai 2020-02-15 07:44:00

Already done, still not working

Related Questions

Sponsored Content

1 Answered Questions

How to get the value of a var within a scope and use it in another file

  • 2019-06-23 23:47:18
  • Linguistics
  • 24 View
  • 0 Score
  • 1 Answer
  • Tags:   javascript node.js

2 Answered Questions

[SOLVED] node.js and express : how to wait for udp response

1 Answered Questions

3 Answered Questions

[SOLVED] Why this error coming while running nodejs server?

  • 2015-08-11 10:37:30
  • Ramesh T
  • 5500 View
  • 0 Score
  • 3 Answer
  • Tags:   node.js

3 Answered Questions

[SOLVED] express.Router() vs. app.get

1 Answered Questions

[SOLVED] Node emitters with express

  • 2014-04-26 14:05:03
  • Koray Güclü
  • 2388 View
  • 3 Score
  • 1 Answer
  • Tags:   node.js express

Sponsored Content