How to split monolithic node.js javascript - Stack Overflow

I have a growing node.js server application, and I'd like to get it split into several files. Here

I have a growing node.js server application, and I'd like to get it split into several files. Here is a working code snippet demonstrating what monolithic server.js roughly looks like:

var express = require('express');
var app = express();
// other initialization code etc

//************** start of part to be moved foo.js

var fooTestData = {data: "data", id:1};

app.get("/foo/ajax", function(req, res) {
    res.json(fooTestData);
});

// more REST stuff and other foo-specific code

//************** end of part to be moved

// more stuff which remains in server.js

// http://localhost:8888/foo/ajax
app.listen(8888);

An ideal answer has two pieces of code with identical functionality: What server.js looks like after moving the indicated part, and what foo.js looks like with copied code and any needed extra code.

I have a growing node.js server application, and I'd like to get it split into several files. Here is a working code snippet demonstrating what monolithic server.js roughly looks like:

var express = require('express');
var app = express();
// other initialization code etc

//************** start of part to be moved foo.js

var fooTestData = {data: "data", id:1};

app.get("/foo/ajax", function(req, res) {
    res.json(fooTestData);
});

// more REST stuff and other foo-specific code

//************** end of part to be moved

// more stuff which remains in server.js

// http://localhost:8888/foo/ajax
app.listen(8888);

An ideal answer has two pieces of code with identical functionality: What server.js looks like after moving the indicated part, and what foo.js looks like with copied code and any needed extra code.

Share Improve this question asked Sep 30, 2013 at 13:28 hydehyde 63k22 gold badges125 silver badges179 bronze badges 4
  • 1 nodejs/api/modules.html – JohnnyHK Commented Sep 30, 2013 at 13:39
  • @JohnnyHK Thanks for the link, it's useful, though I was after a clear before (in question) - after (in answer) example. Which I got. – hyde Commented Sep 30, 2013 at 13:47
  • See my reply to the question here: stackoverflow./questions/18789864/… – Slavo Commented Sep 30, 2013 at 15:25
  • About suggested duplicate: I think that question is a bit different, about how to generally structure a node.js application. This question has much tighter scope, it is about a concrete pattern with example to convert part of an existing monolithic source to a new module. – hyde Commented Oct 1, 2013 at 5:59
Add a ment  | 

2 Answers 2

Reset to default 7

foo.js

var fooTestData = {data: "data", id:1};

exports.setApp = function (app) {

    app.get("/foo/ajax", function(req, res) {
        res.json(fooTestData);
    });

    // more REST stuff and other foo-specific code
};

new sever.js

var express = require('express');
var app = express();

require('./foo.js').setApp(app);

// other initialization code etc


// more stuff which remains in server.js

// http://localhost:8888/foo/ajax
app.listen(8888);

It's a simple example. If you need some things more plex you may want to take a look to other express program on github. It could be a good source of inspiration.

I remend you to have a look at some express-based (MVC) frameworks. Have a look at the list of frameworks. Moreover, you might want to check sails.js (express based framework) if you are doing a webservice and municating with it via RESTfull requests. Please note that sails is not limited to RESTfull requests and can help you scale your project out of the box.

Good luck!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745454240a4628411.html

相关推荐

  • How to split monolithic node.js javascript - Stack Overflow

    I have a growing node.js server application, and I'd like to get it split into several files. Here

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信