我正在使用 node.js,express.js 和 mysql2 创建一个身份验证页面。网页中的 Userid 和 password 出现在 app.post('/login',...) 中。但是当我发布用户名和密码,出现以下错误:
Express
500 TypeError: Not a string or buffer
at Hash.update (crypto.js:209:17)
at app.get.capture.responce (c:\wamp\www\Guru Gobind Singh Ji Site\app.js:455:52)
at callbacks (c:\wamp\www\Guru Gobind Singh Ji
Site\node_modules\express\lib\router\index.js:164:37)
at param (c:\wamp\www\Guru Gobind Singh Ji
Site\node_modules\express\lib\router\index.js:138:11)
at pass (c:\wamp\www\Guru Gobind Singh Ji
Site\node_modules\express\lib\router\index.js:145:5)
at nextRoute (c:\wamp\www\Guru Gobind Singh Ji
Site\node_modules\express\lib\router\index.js:100:7)
at callbacks (c:\wamp\www\Guru Gobind Singh Ji
Site\node_modules\express\lib\router\index.js:167:11)
at app.get.responseJSON (c:\wamp\www\Guru Gobind Singh Ji Site\app.js:33:3)
at callbacks (c:\wamp\www\Guru Gobind Singh Ji
Site\node_modules\express\lib\router\index.js:164:37)
at param (c:\wamp\www\Guru Gobind Singh Ji
Site\node_modules\express\lib\router\index.js:138:11)Express
500 TypeError: Not a string or buffer
at Hash.update (crypto.js:209:17)
at app.get.capture.responce (c:\wamp\www\Guru Gobind Singh Ji Site\app.js:455:52)
at callbacks (c:\wamp\www\Guru Gobind Singh Ji
Site\node_modules\express\lib\router\index.js:164:37)
at param (c:\wamp\www\Guru Gobind Singh Ji
Site\node_modules\express\lib\router\index.js:138:11)
at pass (c:\wamp\www\Guru Gobind Singh Ji
Site\node_modules\express\lib\router\index.js:145:5)
at nextRoute (c:\wamp\www\Guru Gobind Singh Ji
Site\node_modules\express\lib\router\index.js:100:7)
at callbacks (c:\wamp\www\Guru Gobind Singh Ji
Site\node_modules\express\lib\router\index.js:167:11)
at app.get.responseJSON (c:\wamp\www\Guru Gobind Singh Ji Site\app.js:33:3)
at callbacks (c:\wamp\www\Guru Gobind Singh Ji
Site\node_modules\express\lib\router\index.js:164:37)
at param (c:\wamp\www\Guru Gobind Singh Ji
Site\node_modules\express\lib\router\index.js:138:11)
这是代码...
var application_root = __dirname,
express = require("express"),
mysql = require('mysql2'),
path = require("path");
var app = express();
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '123',
database: "bbsbec"
});
app.use(express.bodyParser());
app.use(express.cookieParser('shhhh, very secret'));
app.use(express.session());
// Config
app.configure(function () {
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(application_root, "public")));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.all('/*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
app.post('/login', function(request, response) {
var username = request.body.name;
var password = request.body.passwords;
var hash = require('crypto').createHash('md5').update(password).digest("hex");
console.log(username);
console.log(hash);
var user;
var pword;
connection.query('SELECT * from pass', function(err, rows) {
user = rows[0].userid;
pword= rows[0].password;
if(username == user && password == pword)
{
request.session.regenerate(function(){
request.session.user = username;
response.redirect('http://127.0.0.1/restricted');
});
}
else {
response.redirect('http://127.0.0.1/AdminPanel/error.html');
}
});
});
app.get('/logout', function(request, response){
request.session.destroy(function(){
response.redirect('http://127.0.0.1/Admin-Panel/index.html');
});
});
app.get('/restricted', restrict, function(request, response){
var capture = { responce: 'pass'};
catchjson = JSON.stringify(capture);
response.redirect('http://127.0.0.1/Admin-Panel/admin.html');
});
// Launch server
app.listen(5000, function(){
console.log('Server running...');
});
最佳答案
我正面临我的 nodejs 服务器中的问题。 我发现通过删除以下部分:
app.listen(5000, function(){
console.log('Server running...');
});
服务器将正常工作。
关于javascript - 500 类型错误 : Not a string or buffer in express. js,node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22387694/