node.js错误:抛出新的TypeError('第一个参数必须是字符串或缓冲区')

时间:2018-03-06 13:51:57

标签: node.js

我是Node.js的初学者,我无法找到我做错的事。请点击以下链接查看验证码。

Server has started...
Request for /start received.
About to route a request for /start
Request Handler 'start' was called.
_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 Server.onRequest (H:\Docs\Codes\Web\nodejs beginner\server.js:11:14)
    at emitTwo (events.js:126:13)
    at Server.emit (events.js:214:7)
    at parserOnIncoming (_http_server.js:602:12)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:117:23)

链接到我的代码: https://github.com/PsYcO-kk/nodejs-beginner

1 个答案:

答案 0 :(得分:0)

在router.js中你有:

if(typeof handle[pathname] === 'function'){
    handle[pathname]();
  }

但您没有返回handle[pathname]();

它的工作原理如下:

if(typeof handle[pathname] === 'function'){
    return handle[pathname]();
  }

在编程时尝试使用eslint,这会警告你缺少return语句。