《node.js开发指南》例子报错处理

var http = require('http'); 
var querystring = require('querystring'); 
var util = require('util'); 
 
http.createServer(function(req, res) { 
  var post = ''; 
 
  req.on('data', function(chunk) { 
    post += chunk; 
  }); 
 
  req.on('end', function() { 
    post = querystring.parse(post); 
    res.end(util.inspect(post)); 
  }); 
 
}).listen(3000); 
_http_outgoing.js:645
    throw new TypeError('First argument must be a string or Buffer');
    ^

TypeError: First argument must be a string or Buffer
    at write_ (_http_outgoing.js:645:11)
    at ServerResponse.write (_http_outgoing.js:620:10)
    at IncomingMessage. (...\....js:16:13)
    at emitNone (events.js:106:13)
    at IncomingMessage.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1056:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)


原因是浏览器会请求http://localhost:3000/favicon.ico所以只要在加上

if(req.url=='/favicon.ico')
    return

即可


express 4.x helpers、dynamicHelpers方法处理
app.use(function(req,res,next){
    res.locals.helpers = req.headers
    next();
})
app.locals.inspect=function(obj){  
  return util.inspect(obj, true);  
}  

代码放在路由指定前


其他

const session = require('express-session');
const MongoStore = require('connect-mongo')(session);

你可能感兴趣的:(《node.js开发指南》例子报错处理)