var express = require('express');
var MongoClient = require('mongodb').MongoClient;
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res) {
res.render('index', {authenticated:false,title:' MongoDB example',lang:'My first MongoDB app'});
});
router.get('/login', function(req, res) {
res.render('login', {title:' MongoDB example',lang:'My first MongoDB app'});
});
router.get('/signup', function(req, res) {
res.render('signup', {title:' MongoDB example',lang:'My first MongoDB app'});
});
router.get("/login/:signupEmail",function(req,res){
res.render('login',{signupEmail:req.params.signupEmail})
})
var url = 'mongodb://localhost:27017/myproject';
router.post('/signup',function(req,res){
MongoClient.connect(url, function(err, db) {
console.log("Connected correctly to server");
var collection = db.collection('users');
console.log(req.body.user);//undefined
collection.insert(
req.body['user']
,function(err,docs){
if(err) throw err;
res.redirect('/login/'+docs[0].email);
db.close();
})
});
})
module.exports = router;
req.body.user是undefined 我本以为在这个文件里面添加一段
var bodyParser = require('body-parser');
router.use(bodyParser.json());
router.use(bodyParser.urlencoded({ extended: false }));
这样的代码就能成功,但事实证明还是 undefined
。
请各位大神帮忙解惑。