gpt4 book ai didi

javascript - session 身份验证类型错误: Property 'type' of object # is not a function

转载 作者:太空宇宙 更新时间:2023-11-04 00:57:43 26 4
gpt4 key购买 nike

我正在学习 Node.js 和 Express,并正在研究 Brad Dayley 书中的一些示例。我从书中复制了下面的代码,它使用加密和快速 session 实现了一些基本的 session 身份验证:

var express = require('express');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var crypto = require('crypto');


//encrypt password function
function hashPW(pwd) {
var hash = crypto.createHash('sha256').update(pwd).digest('base64');
return hash.toString();
}

var app = express();

//this is the new way to call this module
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(cookieParser('MAGICString'));

//new way to init session
app.use(session({secret: 'ssshhhhh', resave: false, saveUninitialized: false}));

//see if user is authorized to be here
app.get('/restricted', function(req, res) {
if (req.session.user) {
res.send ('<h2>' + req.session.success + '</h2>' +
'<p>You have entered the restricted section<p><br>' +
'<a href="/logout">logout</a>');
}
else {
req.session.error = 'Access denied';
res.redirect('/login');
}
});

app.get('/logout', function (req, res) {
req.session.destroy(function() {
res.redirect('/login');
});
});

app.get('/login', function(req, res) {
var response = '<form method="POST">' +
'Username: <input type="text" name="username"><br>' +
'Password: <input type="text" name="password"><br>' +
'<input type="submit" value="Submit"></form>';

if (req.session.user) {
res.redirect('/restricted');
} else if (req.session.error) {
response += '<h2>' + req.session.error + '</h2>';
}

res.type = ('html');
res.send(response);
});


//on form submit
app.post('/login', function (req, res) {
//user should be a lookup of req.body.username in database
var user = {name: req.body.username, password:hashPW("myPass")};
if (user.password === hashPW(req.body.password.toString())) {
req.session.regenerate(function() {
req.session.user = user;
req.session.success = 'Authenticated as ' + user.name;
res.redirect('/restricted');
});
} else {
req.session.regenerate(function() {
req.session.error = 'Authentication failed';
res.redirect('/restricted');
});
res.redirect('/login');
}
});

app.listen(8080);

当我运行时,出现错误:

TypeError: Property 'type' of object #<ServerResponse> is not a function
at ServerResponse.send (/home/terry/myProjects/serverLearn/node_modules/express/lib/response.js:129:14)
at app.post.user.name (/home/terry/myProjects/serverLearn/express_auth_session.js:63:9)
at Layer.handle [as handle_request] (/home/terry/myProjects/serverLearn/node_modules/express/lib/router/layer.js:82:5)
at next (/home/terry/myProjects/serverLearn/node_modules/express/lib/router/route.js:110:13)
at Route.dispatch (/home/terry/myProjects/serverLearn/node_modules/express/lib/router/route.js:91:3)
at Layer.handle [as handle_request] (/home/terry/myProjects/serverLearn/node_modules/express/lib/router/layer.js:82:5)
at /home/terry/myProjects/serverLearn/node_modules/express/lib/router/index.js:267:22
at Function.proto.process_params (/home/terry/myProjects/serverLearn/node_modules/express/lib/router/index.js:321:12)
at next (/home/terry/myProjects/serverLearn/node_modules/express/lib/router/index.js:261:10)
at Object.<anonymous> (/home/terry/myProjects/serverLearn/node_modules/express-session/index.js:421:7)


请原谅我的笨蛋,但我就是看不出哪里出了问题。我什至不知道它到底哪里有错误。任何帮助或指示将不胜感激!

最佳答案

行:

res.type = ('html');

错了。您正在尝试在响应对象上设置 Express 不期望的内容。

如果您想手动设置响应的content-type,您可以将此行替换为:

res.header("Content-Type", "text/html");

或者另一种方式,为了完整性:

res.set('Content-Type', 'text/html');

关于javascript - session 身份验证类型错误: Property 'type' of object #<ServerResponse> is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28947184/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com