Friday, 9 June 2017

Nodejs app throws cannot POST /users with body-parser middleware

Here's my app.js file.

    var  express = require('express'),
     bodyParser = require('body-parser'),
     oauthServer = require('oauth2-server'),
     oauth_model = require('./app_modules/oauth_model')


const app = express()

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

var jsonParser = bodyParser.json();

app.oauth = oauthServer({
  model: oauth_model, // See below for specification 
  grants: ['password', 'refresh_token'],
  debug: process.env.OAUTH_DEBUG,
  accessTokenLifetime: 172800,
  refreshTokenLifetime: 172800,
  authCodeLifetime: 120,


});


// Oauth endpoint.

app.all('/oauth/token', app.oauth.grant());


//  User registration endpoint.

app.post('/users', jsonParser, require('./routes/register.js'));


// Get user details.

app.get('/users', app.oauth.authorise(), require('./routes/users.js'));


app.post('/', app.oauth.authorise(), require('./routes/test.js'));

app.use(app.oauth.errorHandler());



app.listen(3000, function () {
  console.log('Mixtra app listening on port 3000!')
})

When I send an invalid json body with POST request to localhost:3000/users the request goes to register.js and the validation code works there.

but strangely when I send valid JSON body, it says "Cannot POST /users" with a 404 Not Found HTTP status code and nothing in terminal log.

Note: I'm using postman to send the api requests.

It would be really great if someone could help me with this.

Thanks,

Joy



via Joy

No comments:

Post a Comment