我想做一个上传表格。这部分已经完成并且可以工作...但是现在,我想输入与项目中相同的安全验证。

我有这个:

 app.post('/me/profile/picture/upload',function(req, res){

    req.form.on('part', function(part){
        // transfer to s3 etc
        console.log('upload %s %s', part.name, part.filename);
        var out = fs.createWriteStream(tempUploadPath + part.filename);
        part.pipe(out);
    });

    req.form.on('close', function(){
        res.respond("Success!", 200);
    });

});


但我想要这个:

    app.post('/me/profile/picture/upload', passportToken.authenticate('bearer', { session: false }),function(req, res){

    req.form.on('part', function(part){
        // transfer to s3 etc
        console.log('upload %s %s', part.name, part.filename);
        var out = fs.createWriteStream(tempUploadPath + part.filename);
        part.pipe(out);
    });

    req.form.on('close', function(){
        res.respond("Success!", 200);
    });

});


但是,当我添加此护照代码时,在传输文件时冻结。

最佳答案

回调将请求和响应对象作为参数。您必须将它们传递给身份验证功能。尝试这样的事情:

app.post('/me/profile/picture/upload', function (req, res) {

    passportToken.authenticate('bearer', { session: false }), function(){

        req.form.on('part', function(part){
            // transfer to s3 etc
            console.log('upload %s %s', part.name, part.filename);
            var out = fs.createWriteStream(tempUploadPath + part.filename);
            part.pipe(out);
        });

        req.form.on('close', function(){
            res.respond("Success!", 200);
        });

    })(req, res)

});

关于node.js - 带有Express(NodeJS)上传表单的PassportJS,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18952801/

10-11 05:41