Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js on Openshift gives error 400 but works properly on local machine

My Node + MongoDB based REST API works fine on local Node server and returns the correct output. But Express(probably!) returns error 400 Bad Request when deployed on Openshift. Any idea how I might debug the app? I tried putting some console.log statements in the offending method but even those were not called.

BTW, some sample code:

app.js:

var express = require('express');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var app = express();

var userController = require('./Controllers/user');
var authController = require('./Controllers/auth');
mongoose.connect(process.env.OPENSHIFT_MONGODB_DB_URL + 'dbname');
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
app.use(function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With , content-type, Authorization ,x-access-token ,' +
    'x-auth-email ,x-auth-password');
next();
});

app.post('/signin',authController.authorize,userController.signin);

controllers/auth.js:

var passport = require('passport');
var User = require('../Models/user');
var LocalStrategy = require('passport-local').Strategy;

passport.use(new LocalStrategy(function(email,password,callback) {
console.log('passport called');
User.findOne({email:email},function(err, user){
    console.log('email at passport: ' + email);
    if(err){
       return callback(err);}


   if(!user){return callback(null,false);}

    user.matchPassword(password,function(err,isMatch){
        if(err){return callback(err);}

        if(!isMatch){return callback(null,false);}

        console.log('isMatch: ' + isMatch);
        return callback(null,user);
      });
    });
 }));

 exports.authorize = passport.authenticate('local', { session : false });

controllers/user.js:

 var User = require('../Models/user');
 var jwt = require('jsonwebtoken');
 exports.signin = signin= function (req,res){
User.findOne({email:req.body.email},function(err, user){
    if(err){res.send(err);}


    console.log(user);
        user.token = jwt.sign(user.password, 'SECRET_KEY');
        user.save(function (err,user) {
            if (err) {
                res.send(err);
            }
            res.json({email:user.email,token:user.token});
        })
});
};

Calling the signin route with POST in Postman returns expected data in json in Local server but returns HTTP 400 Bad Request on deploying on Openshift. I even tried downgrading express to the version on Openshift but the issue remains the same.

like image 746
Shantanu Paul Avatar asked Jan 11 '16 17:01

Shantanu Paul


Video Answer


1 Answers

Solved! Apparently, passport.js was the culprit. Passport takes (username,password,function()) while I was passing (email,password,function()).

The final code of auth.js:

 passport.use(new LocalStrategy({usernameField: 'email',
                            passwordField: 'password'},
      (function(username,password,callback) {
like image 95
Shantanu Paul Avatar answered Sep 25 '22 05:09

Shantanu Paul