i creating simple node.js application , following 4 files created . file1: 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);
file2: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 + "recieved"); response.writehead(200,{"content-type":"text/plain"}); var content = route(handle, pathname) console.log("contents is" + content); response.write("content " +content); response.end(); } http.createserver(onrequest).listen(8888) console.log("server has started"); } exports.start = start;
file 3:router.js
function route(handle, pathname) { console.log("about route request for" + pathname); if (typeof handle[pathname] === 'function') { return handle[pathname](); } else { console.log("no request handler found "+ pathname); return "404 not found"; } } exports.route = route;
file 4:requesthandlers.js
function start() { console.log("request handler 'start' called "); return "hello start"; } function upload() { console.log("request handler 'upload' called"); return "hello upload"; } //this allows wire request handlers router, giving our router route exports.start = start; exports.upload = upload;
now when start index.js on terminal , request http://localhost:8888/start on browser getting output as:
$ node index.js server has started request for/recieved route request for/ request handler 'start' called contents isundefined
so in server.js line //var content = route(handle, pathname) not returning proper value "hello start" . have started leaning please 1 can tell wrong ?
Comments
Post a Comment