Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在节点中检索通过HTTPS发送的表单数据?_Javascript_Node.js_Post_Express_Https - Fatal编程技术网

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

Javascript 如何在节点中检索通过HTTPS发送的表单数据?,javascript,node.js,post,express,https,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.b

我是一个有点后端安全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。那么数据保存在哪里呢

任何帮助都将不胜感激

谢谢


James

req.body
仅在您链接到适当的中间件以解析请求正文时才存在。我建议如下:

app.use(express.urlencoded());
app.use(express.json());
您经常会看到使用
express.bodyParser()
,但我建议避免这样做,因为它还包括
express.multipart()
,它已被弃用,并且在express更新其对Connect的依赖关系时将消失。如果需要解析多部分表单数据,请查看或

我不认为您的问题与HTTPS有任何关系:解析请求正文在HTTP和HTTPS中是相同的过程。

好的,我明白了

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

在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...

您是否正在使用
bodyParser
?嗨@Ethan Brown,是的,我正在使用bodyParser。。。我将交换它们,然后再试一次……此外,确保在任何依赖它们的路由之前注册这些中间件。嗯……如果您使用的是
bodyParser
,则
req.body
对象应该可用……所以我想知道Ray是否没有连接到某个对象(您将它链接到了错误的位置)。这应该是第一批链接进来的中间产品之一…谢谢Ethan,@RayStantz发现了一些东西。。。见下文!:)