我的Node + MongoDB
基础REST API在本地Node
服务器上运行正常,并返回正确的输出.但是Express
(可能!)400 Bad Request
在Openshift上部署时会返回错误.知道如何调试应用程序吗?我尝试console.log
在违规方法中加入一些陈述,但即使是那些也没有被调用.
BTW,一些示例代码:
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);
控制器/ 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 });
控制器/ 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}); }) }); };
signin
在Postman中使用POST 调用路由返回本地服务器中的json中的预期数据,但HTTP 400 Bad Request
在Openshift上部署时返回.我甚至尝试降级express
到Openshift上的版本,但问题仍然存在.