私はnode.jsが初めてです。プロジェクトに server.js 、 router.js 、 index.js 、および requestHandler.js ファイルがあります。以下のようなURLで開始またはアップロードを渡します
http://localhost:8888/start
URL から pathName を取得し、以下のようにサーバーの router.js に渡します。
サーバー.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.");
route(handle,pathname);
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
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);
}
}
exports.route = route ;
index.js :
var server = require("./Server");
var router = require("./router");
var requestHandlers = require("./requestHandler");
var handle={}
handle["/"]=requestHandlers.start;
handle["/start"]=requestHandlers.start;
handle["/upload"]=requestHandlers.upload;
server.start(router.route,handle);
requestHandler.js :
function start(){
console.log("Request handler 'start' was called.");
}
function upload(){
console.log("Request handler 'upload' was called.");
}
exports.start=start;
exports.upload=upload;
私は間違った結果を得ます:
PS C:\Program Files\nodejs> node ..\..\Users\private\Desktop\hello\index.js
Server has started.
Request for /start received.
About to route a request for /start
No request handler found for undefined
Request for /favicon.ico received.
About to route a request for /favicon.ico
No request handler found for undefined
Request for /favicon.ico received.
About to route a request for /favicon.ico
No request handler found for undefined
これの代わりに :
Server has started.
Request for /start received.
About to route a request for /start
Request handler 'start' was called.
何が問題ですか?何か案が?