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 ?
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 ?
Share Improve this question asked Jun 7, 2017 at 8:21 MonteiroMonteiro 2175 silver badges17 bronze badges 2- Thank you @George, it worked. If you want, you can put that as an answer, I will upvote and mark as the right ment. – Monteiro Commented Jun 7, 2017 at 8:25
- And sorry for the mistake guys, I am new with this, and sometimes (a lot of) happens those newbie mistakes. – Monteiro Commented Jun 7, 2017 at 8:26
1 Answer
Reset to default 3You're passing the route
to the start
function but a parameter isn't defined, you need to add one.
function start(route) {
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;");
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745421632a4626989.html
评论列表(0条)