Javascript node.js中的Web服务器路径错误
我尝试使用node.js作为Web服务器,我的源代码分为四个模块Javascript node.js中的Web服务器路径错误,javascript,node.js,webserver,Javascript,Node.js,Webserver,我尝试使用node.js作为Web服务器,我的源代码分为四个模块 index.js server.js 路由器.js requestHandlers.js 代码的目的是在/、/start和/upload中显示不同的页面,而如果我键入另一个路径,则会出现错误404。但是,当我将请求发送到服务器时,就会出现错误 这是我的错误: > Server has started. Request for /start received. About to route a > request f
- index.js
- server.js
- 路由器.js
- requestHandlers.js
> 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)
index.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);
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;
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;
server.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;
router.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);
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;
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;
requestHandlers.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);
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;
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()
函数返回任何内容:
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";
}
}
功能路由(句柄、路径名){
log(“即将路由对“+路径名”的请求);
if(句柄类型[路径名]=“函数”){
返回句柄[pathname]();//看起来您在成功时不会从route()函数中返回任何内容:
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";
}
}
功能路由(句柄、路径名){
log(“即将路由对“+路径名”的请求);
if(句柄类型[路径名]=“函数”){
return handle[pathname]();//您可以通过向函数调用handlepathname添加一个return来更新route函数,然后它就可以工作了
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;
您可以通过向函数调用handlepathname添加一个return来更新route函数,它应该可以工作
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;
抱歉,看起来我们是同时回答的。不过接得好。抱歉,看起来我们是同时回答的。不过接得好。