如何检索节点中通过HTTPS发送的表单数据?

时间:2014-03-10 19:22:54

标签: javascript node.js post express https

我有点像后端安全n00b,所以如果我遗漏了一些明显的东西,请保持温和:

当我在节点中通过HTTP获取值时,表单数据位于请求对象req.body.{name of input element}

通过HTTPS,req.body似乎不存在。我已经尝试退出req对象,但我无法在那里看到它。我做错了什么?

function checkUser(req, res) {
    console.dir(req);
    //if passwords don't match
    if (req.body.password !== req.body.confirm_password) {
        return false;
    }
    return true;
}

app.post('/register', function(req, res) {

    if (checkUser(req, res)) {
        createUser(req, res)
        res.redirect('/browse?p=0');
    }
    res.render('register', {
        error: 'Passwords did not match'
    })
});

一旦进入checkUser方法,它就会崩溃,说没有定义req.body。那么数据保存在哪里?

任何帮助将不胜感激......

由于

詹姆斯

2 个答案:

答案 0 :(得分:0)

req.body仅在链接到适当的中间件以解析请求正文时才存在。我推荐以下内容:

app.use(express.urlencoded());
app.use(express.json());

您经常会看到express.bodyParser()正在使用,但我建议避免使用它,因为它还包含已被弃用的express.multipart(),并且当Express更新其对Connect的依赖时将消失。如果您需要解析多部分表单数据,请查看BusboyFormidable

我认为您的问题与HTTPS无关:解析请求正文与HTTP和HTTPS中的进程相同。

答案 1 :(得分:0)

好的,我明白了......

我按正确的顺序调用了一些东西,但是我将所有的护照内容(以及相应的中间件)都包含在模块文件中。由于可能是范围或竞争条件,它在路由和控制器执行之前没有注册中间件。

web.js中的

app = express();
app.settings.env = 'development';
app.engine('dust', dustjs.dust({
    cache: false
}));
app.set('view engine', 'dust');
app.set('views', __dirname + '\\views');

//Middleware
app.use(express.methodOverride());
app.use(express.favicon(__dirname + '/public/images/favicon.ico', {
    maxAge: 2592000000
}));

app.use(app.router);

//Environment Variables
//app.configure('development', function() {
app.use(express.errorHandler({
    dumpExceptions: true,
    showStack: true
}));
dustjs.isDebug = true;


auth = require('./modules/auth/auth').auth(app);
auth.js中的

module.exports.auth = function(app) {
//verification using passport.js
passport = require("passport");
express = require('express');
LocalStrategy = require('passport-local').Strategy;
FacebookStrategy = require('passport-facebook').Strategy;
TwitterStrategy = require('passport-twitter').Strategy;

app.use(express.cookieParser());
app.use(express.urlencoded());
app.use(express.json());
app.use(express.session({
    secret: 'SECRET'
}));
app.use(passport.initialize());
app.use(passport.session());



passport.use(new LocalStrategy(
etc...
相关问题