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.

4
  • 1
    nodejs.org/api/modules.html 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. Commented Sep 30, 2013 at 13:47
  • See my reply to the question here: stackoverflow.com/questions/18789864/… 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. Commented Oct 1, 2013 at 5:59

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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!

3 Comments

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 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.
Ah, right. Annoying, that :-). Yeah, useful info and links, thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.