javascript - 500 类型错误 : Not a string or buffer in express. js,node.js

标签 javascript mysql node.js express

我正在使用 node.js,express.jsmysql2 创建一个身份验证页面。网页中的 Useridpassword 出现在 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/

相关文章:

javascript - Angularjs keyup 与带有西里尔字母的按键

javascript - 如何在 Rickshaw/D3 中设置 x 轴的最小值/最大值?

javascript - Express 呈现index.html 但不呈现其他页面

php - 如何单独交叉引用数据库表?

MySQL 到 PostgreSQL 转换器

node.js - 控制 Session 从 express 和 connect 中间件开始

javascript - 在 Sequelize Hooks 中使用 Promise

node.js - 如何捆绑 "express"node.js 应用程序? (使用网页包)

javascript - 使用 Chrome 和 Safari 打印时发票页脚 CSS 在打印中丢失

php - 我对 MySQL 插入是否成功的测试不起作用