节点中的 Web 服务器.js路径中的错误

Webserver in node.js error in path

本文关键字:路径 错误 js Web 节点 服务器      更新时间:2023-09-26

我正在尝试使用node.js作为Web服务器,我的源代码分为四个模块。

  • 索引.js
  • 服务器.js
  • 路由器.js
  • 请求处理程序.js

代码的目的是显示在/、/start 和/upload 不同的页面中,而如果我输入另一个路径,我应该会收到错误 404。但是,一旦我将请求发送到服务器,我就会收到错误。

这是我的错误:

> Server has started. Request for /start received. About to route a
> request for /start Request handler 'start' was called.
> 
> http.js:852
>     throw new TypeError('first argument must be a string or Buffer');
>           ^ TypeError: first argument must be a string or Buffer
>     at ServerResponse.OutgoingMessage.write (http.js:852:11)
>     at Server.onRequest (/Users/Alessio/Desktop/Circolare/server.js:12:14)
>     at Server.emit (events.js:98:17)
>     at HTTPParser.parser.onIncoming (http.js:2113:12)
>     at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:122:23)
>     at Socket.socket.ondata (http.js:1971:22)
>     at TCP.onread (net.js:528:27)

索引.js

var server = require("./server");
var router = require("./router");
var requestHandlers=require("./requestHandlers");
var handle={}
handle["/"]=requestHandlers.start;
handle["/start"]=requestHandlers.start;
handle["/upload"]=requestHandlers.upload;
server.start(router.route, handle);

服务器.js这是我遇到错误的地方

var http = require("http");
var url = require("url");
function start(route, handle) {
  function onRequest(request, response) {
    var pathname = url.parse(request.url).pathname;
    console.log("Request for " + pathname + " received.");
    response.writeHead(200, {"Content-Type": "text/plain"});
    var content = route(handle, pathname); //Here the ERROR
    response.write(content);
    response.end();
  }
  http.createServer(onRequest).listen(8888);
  console.log("Server has started.");
}
exports.start = start;

路由器.js

function route(handle, pathname){
    console.log("About to route a request for " + pathname);
    if(typeof handle[pathname] === 'function'){
        handle[pathname]();
    }
    else{
        console.log("No request handler found for " + pathname);
        return "404 Not Found";
    }
}
exports.route = route;

请求处理程序.js

function start(){
    console.log("Request handler 'start' was called.");
    return "Hello Start";
}
function upload(){
    console.log("Request handler 'upload' was called.");
    return "Hello Upload";
}
exports.start=start;
exports.upload=upload;

看起来你没有从你的route()函数中return任何关于成功的东西:

function route(handle, pathname){
    console.log("About to route a request for " + pathname);
    if(typeof handle[pathname] === 'function'){
        return handle[pathname]();  // <-- probably need a `return` statement here...
    }
    else{
        console.log("No request handler found for " + pathname);
        return "404 Not Found";
    }
}

您可以通过向函数调用 handlepathname 添加返回来更新路由函数,然后它应该可以工作。

function route(handle, pathname){
    console.log("About to route a request for " + pathname);
    if(typeof handle[pathname] === 'function'){
       return handle[pathname]();
    }
    else{
        console.log("No request handler found for " + pathname);
        return "404 Not Found";
    }
}
exports.route = route;