2014-03-13 72 views
1

我使用node.js,express.jsmysql2创建验证页面。 用户ID,并从网页密码app.post(“/登录”,...)。但是来了,当我张贴的用户名和密码,以下错误出现:500 TypeError:不是express.js中的字符串或缓冲区,node.js

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...'); 
}); 
+1

发布的密码是什么?这绝对是一个字符串? –

+0

是req.body.passwordS对不对?我有点反感复数 – lascort

+0

是的,这是一个字符串 – jaassi

回答

0

我正好面对我的nodejs服务器中的问题。 我发现通过删除以下部分:

app.listen(5000, function(){ 
console.log('Server running...'); 
}); 

服务器将正常工作。

相关问题