1

I have created a small express app. I just trying to test the post method. But it's not working at all. according to me all are fine. But I couln't figure out the issue. any one help me here please?

my server.js file :

var express = require('express');
var bodyParser = require('body-parser');
var Post = require('./models/post');

var app = express();
app.use(bodyParser.urlencoded({
    extended: true
}));

app.use(bodyParser.json());

app.get('/api/posts', function( req, res) {
    res.json([
        {
            username:"dickeyxxx",
            body : "node rocksxx"
        }
    ])
});


app.post('/api/posts', function( req, res, next) {

    var post = new Post({
        username:req.body.username,
        body:req.body.body
    })

    post.save(function(err, post){
        if(err) { return next }
            res.json(201,post);
    })

})



app.listen(5000, function(){
    console.log('Server Listening on', 5000);
})

my post.js :

var db = require('../db');
var Post = db.model('Post', {
    username:{type:String, required:true},
    body:{type:String,required:true},
    date:{type:Date, required:true,default:Date.now}
});

module.exports = Post;

my db.js:

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/social', function(){
    console.log( 'mongoose connected' ); //i am getting consoled
});

module.exports = mongoose;

my try with postman :

postman screen shot

any one help me here?

3gwebtrain
  • 14,640
  • 25
  • 121
  • 247

2 Answers2

3

Your res.json(201,post); is wrong.

If you want to send status 201 then use

res.status(201).json(post);

see http://expressjs.com/en/api.html#res.json

Molda
  • 5,619
  • 2
  • 23
  • 39
2

An error like that is normally a case that Postman can't actually reach the client. Its not a error typical of what express returns.

When you start your express app, do you actually see

Server Listening on 5000

If so, does the GET endpoint work?

If the above is true then the typical way to get an error like this is your express app actually exiting due to an uncaught exception. Are you seeing any errors in the console when you send the postman request?

After these checks add a console.log('POST endpoint called') to the POST endpoint and check that it is actually being called.

Your POST endpoint also has issues.

post.save(function(err, post){
    if(err) { return next(err) } // You need to actually return the called next function, not the function itself. 
    return res.json(201,post); // It is commonplace to return the res here to avoid hitting any further code that you might add later.
})

And while its not in your code example (maybe for brevity) make sure you have some last endpoint that pick up any errors or 404's so that you always return a response.

app.all('*', function (req, res, next) {
    return res.status(404).json({success: false, message: 'Route \'' + req.url + '\' is invalid.'});
});
app.use(function(err, req, res, next) {
    return res.status(500).json(err);
}); 
Ian Belcher
  • 5,583
  • 2
  • 34
  • 43
  • some times when i delete all files in side of my `data\db` - it seem to works. is it mean that, each request i need to clear that folder or what is wrong here? – 3gwebtrain Jun 30 '16 at 07:59
  • In that case, the issue is likely you're getting an error from mongo as you the item already exists or something similar, and it returns an error. Because you're not actually _calling_ `next`, express never returns a result and hangs, which is why postman never actually gets any data. Add `next(err)` and the error endpoint above and it should give you the error. – Ian Belcher Jun 30 '16 at 08:07
  • And as per molda's answer, you'll need to update your json call as well if you want the proper status codes to be returned but from what you're saying it appears as though you are getting a response on those occasions. – Ian Belcher Jun 30 '16 at 08:25
  • It is very glad to hear from all your suggestions and ideas. you have turned me in the right path. very thanks to you. – 3gwebtrain Jun 30 '16 at 08:42