簡體   English   中英

500 TypeError:在express.js,node.js中不是字符串或緩沖區

[英]500 TypeError: Not a string or buffer in express.js,node.js

我正在使用node.js,express.jsmysql2創建一個身份驗證頁面。 來自網頁的用戶名密碼都在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...');
});

服務器將正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM