最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I pass the http server variable between layers and not use it as a require statement in a different file in

programmeradmin2浏览0评论

I've created a nodeJS application which needs to create a server which works OK like the following in file server.js:

http.createServer(app).listen(app.get('port'), function (err) {
    if (err) {
        console.error(err);
    } else {
        runProcess();
        console.log('Server listening on port ' + app.get('port'));
    }
});

Now I need to pass this server to some file and my app built like the following:

server.js 
 app.js
  routes.js
   action.js

So I did it quick like the following,Is there a better way to do that?

This is the server.js after the changes:

var server = http.createServer(app);
app.setServer(server);
server.createServer(app).listen(app.get('port'), function (err) {
    if (err) {
        console.error(err);
    } else {
        runProcess();
        console.log('Server listening on port ' + app.get('port'));
    }
});

In the app.js I've the router file and I did like the following:

app.use('/', routes.router, function (req, res, next) {
    next();
});

app.setServer = function(http) {
    routes.setServer(http);
}

module.exports = app;

This code I put in the router.js file:

module.exports = {
    router: applicationRouters,
    setServer: function(http) {
        //here I set server to the file action.js
        action.setServer(http);
    }
}

And in the action js, I did the following:

module.exports = {
    setServer: function(http) {
...

I need to pass the http between layers and not use it as require in different file/module. How do I do that in node.js?

I've created a nodeJS application which needs to create a server which works OK like the following in file server.js:

http.createServer(app).listen(app.get('port'), function (err) {
    if (err) {
        console.error(err);
    } else {
        runProcess();
        console.log('Server listening on port ' + app.get('port'));
    }
});

Now I need to pass this server to some file and my app built like the following:

server.js 
 app.js
  routes.js
   action.js

So I did it quick like the following,Is there a better way to do that?

This is the server.js after the changes:

var server = http.createServer(app);
app.setServer(server);
server.createServer(app).listen(app.get('port'), function (err) {
    if (err) {
        console.error(err);
    } else {
        runProcess();
        console.log('Server listening on port ' + app.get('port'));
    }
});

In the app.js I've the router file and I did like the following:

app.use('/', routes.router, function (req, res, next) {
    next();
});

app.setServer = function(http) {
    routes.setServer(http);
}

module.exports = app;

This code I put in the router.js file:

module.exports = {
    router: applicationRouters,
    setServer: function(http) {
        //here I set server to the file action.js
        action.setServer(http);
    }
}

And in the action js, I did the following:

module.exports = {
    setServer: function(http) {
...

I need to pass the http between layers and not use it as require in different file/module. How do I do that in node.js?

Share Improve this question edited Oct 8, 2015 at 14:31 George Stocker 57.9k29 gold badges181 silver badges238 bronze badges asked Oct 1, 2015 at 7:11 user4209821user4209821 6
  • In your new server.js, you have server.createServer(app). Is that a typo? – anubina Commented Oct 5, 2015 at 14:27
  • A few issues: The menter above points out what appears to be a typo; meaning this code doesn't actually work; secondly; are you looking for 'style points' or is there a legitimate reason the 'working' approach doesn't actually work for you? – George Stocker Commented Oct 6, 2015 at 19:53
  • @GeorgeStocker - I need to pass it (the http) between layers and not using it as require in other file....any idea what is right way to do that ? – user4209821 Commented Oct 8, 2015 at 7:18
  • @GeorgeStocker - can you please remove the hold, I need help here and this is not trivial ....I want to put on it bounty which maybe can help me to progress with my problem since as you see the post was read by many pepole but only one answer which doesnt help to much... – user4209821 Commented Oct 8, 2015 at 13:41
  • @Mark Your title wasn't very clear; nor was your question. I improved both and re-opened it. Next time you ask a question pay special attention to both parts, as that can keep your question from being closed. – George Stocker Commented Oct 8, 2015 at 14:32
 |  Show 1 more ment

2 Answers 2

Reset to default 6

You don't need to pass the server to routes. Just include them prior to creating the server:

someroute.js

var router = require("express").Router();
router.use('/', routes.router, function (req, res) {
    return res.json();
});

module.exports = router;

server.js

var app = express();
....
var someroute = require("./routes/someroute");
app.use("/someroute", someroute);
server.createServer(app);

Update to the answer based on feedback:

In your server.js file you probably have a variable to hold the socket.io instance when it's initialized, let's call it io.

You will initialized the io variable like this:

//example taken from socket.io front page
var io = require('socket.io')(80); 

When registering middleware you can attach this variable to req and use it in the routes:

//this goes prior to your route registration
app.use(function(req, res, next) {
    req.io = io;
});

In the routes you will refer to req.io as a socket instance:

router.use('/', routes.router, function (req, res, next) {
    req.io.emit("something", {});
});

If I get you right, all you want to do is pass an instance to other modules, without having to require or re-initialize them.

server.js

module.exports = (function () {
    // create the server
    var http = require('http'),
        express = require('express'),
        app = express(),
        server = http.createServer(app);

    server.listen(80, function () {
        // require and invoke the modules that need this http/server instance here
        require('router')(server);
        require('action')(server);
    });
}());

router.js

module.exports = function (server) {
    // use server here..
};

action.js

module.exports = function (server) {
    // use server here..
};

So what you do is, first declare and initialize the object within your main module, you want to pass around. Then in the secondary modules, you just declare the module with a signature that takes this object as an argument. Finally, you'll require them only once within your main module (server.js in this case).

Note 1: I renamed the http variable to server to prevent confusion with the http built-in module.

Note 2: Don't forget to require those modules after you fully initialize the object. e.g. if this is a http server as in your example, you should better require other modules after you create the server and within the listen callback.

Note 3: Your code; after changes, does not look correct. You call var server = http.createServer() then you call server.createServer() again.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论