admin管理员组文章数量:1425804
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;");
}
本文标签: javascriptRouter is not defined NodejsStack Overflow
版权声明:本文标题:javascript - Router is not defined Node.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745421631a2657925.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论