Wednesday 7 June 2017

Router is not defined Node.js

I am new with Node.js, and I am trying to create an HTTP server, but for some reason, when I try to put the router for purchase URL request, it doesn't work.

My code :

Server.js

var url = require("url");
var http = require("http"); 


function start() {
function onRequest(request, response) { 
    var pathname = url.parse(request.url).pathname; 
    console.log("Request received.");       

    route(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;

Index.js

var server = require("./server");
var router = require("./router");

server.start(router.route);

Router.js

function route(pathname) {
    console.log("About to route a request for " + pathname);
}

exports.route = route;

When trying to start the server through the Node.js, it says the follow error :

route is not defined

route(pathname);

How can I make this work ?



via Monteiro

No comments:

Post a Comment