4

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.

hyde
  • 60,639
  • 21
  • 115
  • 176
  • 1
    http://nodejs.org/api/modules.html – JohnnyHK Sep 30 '13 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 Sep 30 '13 at 13:47
  • See my reply to the question here: http://stackoverflow.com/questions/18789864/node-js-express-global-modules-best-practices-for-application-structure/18790200#18790200 – Slavo Sep 30 '13 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 Oct 01 '13 at 05:59

2 Answers2

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 complex you may want to take a look to other express program on github. It could be a good source of inspiration.

hyde
  • 60,639
  • 21
  • 115
  • 176
peernohell
  • 808
  • 4
  • 12
1

I recommend 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 communicating 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!

Christofer Eliasson
  • 32,939
  • 7
  • 74
  • 103
eAbi
  • 3,220
  • 4
  • 25
  • 39
  • 1
    While those links are useful, thanks, this does not really answer the question. If you want to share information like this related to a Stack Overflow question, the right place to do it is in comments, not as an answer. – hyde Sep 30 '13 at 13:58
  • 1
    @hyde I wanted to write this as a comment, but writing a comment requires 50 reputation which I don't have. I wanted to be helpful, and I hope I was, and so not adding the answer nor the comment (because of reputation) was not an option. – eAbi Sep 30 '13 at 16:35
  • Ah, right. Annoying, that :-). Yeah, useful info and links, thanks. – hyde Oct 01 '13 at 04:20