javascript - Router is not defined Node.js - Stack Overflow

I am new with Node.js, and I am trying to create an HTTP server, but for some reason, when I try to put

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
Add a ment  | 

1 Answer 1

Reset to default 3

You'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

相关推荐

  • javascript - Router is not defined Node.js - Stack Overflow

    I am new with Node.js, and I am trying to create an HTTP server, but for some reason, when I try to put

    5小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信