【发布时间】:2018-05-25 17:25:11
【问题描述】:
app.get("/auth/google", passport.authenticate("google", {
session: false,
scope: ["profile", "email"]
}));
passport.use(new GoogleStrategy({
clientID: " x",
clientSecret: " y",
callbackURL: "http://localhost:3000/auth/google/callback"
},
function(accessToken, refreshToken, Gprofile, done) {
process.nextTick(function() {
Gdata = {
'id': Gprofile.id,
'name': Gprofile.displayName,
'profileImage': Gprofile.photos[0].value,
'gender': Gprofile.gender,
'email': Gprofile.emails[0].value
};
console.log('Gdata recieved');
console.log('--------------------------------------------------------------------------');
return done(null, true); //It seems redirects to failureRedirect or successRedirect.
});
}
));
app.get("/auth/google/callback", passport.authenticate('google', {
session: false,
failureRedirect: "/google_callback_fail",
successRedirect: "/reports"
}));
app.get("/reports", function(req, res) {
// ------------DB
var db = require('../model/connection.js');
var stmt;
var name = Gdata.name.split(" ");
stmt = "INSERT INTO User( `first_name`, `last_name`, `email`) VALUES (?,?,?)";
var x = new Promise(function(resolve, reject) {
var sentToCJS;
db.query(stmt, [name[0], name[1], Gdata.email], function(err, rows) {
if (err) reject(err);
PAYLOAD = { userID: rows.insertId };
ENCODED_JWT = _jwt.sign(PAYLOAD, 'secret', { expiresIn: EXP_TIME });
console.log("1 ENCODED_JWT:", ENCODED_JWT);
sentToCJS = {
'jwt': ENCODED_JWT,
'name': Gdata.name,
'profileImage': Gdata.profileImage,
'gender': Gdata.gender,
'email': Gdata.email
};
console.log("sentToCJS:", sentToCJS);
//creates persistant cookie.
res.cookie("jwt", ENCODED_JWT, {
maxAge: 604800, //7days to ms
httpOnly: true,
sameSite: true,
signed: true //detects if user has modified the cookie
});
resolve(sentToCJS);
});
});
x.then(function(sentToCJS) {
res.render('reports', sentToCJS);
});
x.catch(function(error) {
console.log("error", error);
});
期望的结果:服务器响应 cookie 和渲染('reports', sentToCJS); res.cookie 和 res.render 需要工作。
错误:发送后无法设置标题。 在 SendStream.headersAlreadySent
如何将 res.cookie 和 res.render 一起发送或以多个响应发送? express 似乎不可能,但 node.js res.write 或 writeHead 方法可能。
【问题讨论】:
-
这两个语句是单个响应的一部分。两次响应是什么意思?
-
@Muthukumar 我更新了错误信息
标签: node.js http express cookies response