节点中的 Web 服务器.js路径中的错误
Webserver in node.js error in path
我正在尝试使用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;
相关文章:
- javascript window.location在检查firebug时给了我一个错误的url路径
- 最短路径算法js错误
- AngularFire 0.5美元移除.错误:Firebase.child失败:第一个参数是无效路径
- windows文件系统中的nodejs路径错误4058 ENOENT
- 路径错误中的非法字符
- 使用Raphael.js绘制双动画路径错误
- Javascript /npm安装路径错误
- 流星,NGINX子路径错误
- Grunt Dist文件夹相对路径错误
- 为什么在Durandal Get started教程中,text.js的请求路径错误?
- SVG路径错误"d"属性
- 弹出asp.net页面,javascript和路径错误404
- 在Ember项目中包含外部JS文件的路径错误(不确定)
- Extract-text-webpack-plugin输出CSS文件,图像路径错误
- Nodejs中的相对路径错误
- Webpack,新块加载路径错误
- 使用Ajax访问其他目录中的文件时出现问题,是我指定的路径错误吗
- IE8上的jQuery选择器路径错误
- 木偶.js需要.js模板路径错误
- Firebase child_added事件返回无效路径错误