0

I am planning to develop only rest api using express js, I looked into lot of boilerplate projects. None of them provide modularity. Modularity I mean all code related to articles module need to be in article folder then I can drag and drop that.

I saw MEAN somewhat close to that but it has client side (angular related) code in that. I need pure rest api framework.

2
  • Can you please be a little bit more specific? In general you are right, you do not need the full MEAN stack, a simple express app is enough to build a modular rest api. What is your question? Commented Aug 29, 2016 at 6:55
  • @Jonas As express js doesn't provide any best practise to structure your application, I am looking for some boilerplate code. All boilerplate I saw doesn't have modular structure. That means I can't keep all files related to one module in one folder and disable or enable it. Commented Aug 30, 2016 at 9:43

2 Answers 2

3

To me it doesn't sound like you want to use a MEN stack, I do not see a reason to use MongoDB in your question. You can write modular express apps e.g. like this:

Assuming you have three modules in three different folders moduleA, moduleB and moduleC. Each folder contains its respective logic and provides some RESTful routes to the outside world. In express you would create one separate Router for each module like this:

ModuleA:

/* moduleA/routes.js */
var express = require('express');
var router = express.Router();

... // add all routes of moduleA

module.exports = router;

ModuleB:

/* moduleB/routes.js */
var express = require('express');
var router = express.Router();

... // add all routes of moduleB

module.exports = router;

ModuleC:

/* moduleC/routes.js */
var express = require('express');
var router = express.Router();

... // add all routes of moduleC

module.exports = router;

And then you would have one main app.js file in your root folder where you enable and disble the single modules by mounting them into the main express app:

/* app.js */
var express = require('express');
var moduleA = require('./moduleA/routes');
var moduleB = require('./moduleB/routes');
var moduleC = require('./moduleC/routes');

var app = express();

... // add your main app's middlewares

app.use('/moduleA', moduleA);
app.use('/moduleB', moduleB);
// app.use('/moduleC', moduleC);

app.listen(3000);

In this example the modules moduleA and moduleB are enabled and are reached by the routes /moduleA/* and /moduleB/* respectively. The module moduleC is disabled as we commented it out.

If you have questions please leave a comment.

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

Comments

2

It sounds like you want to use a "MEN" stack, which is MongoDB (for backend), Express, and Node.JS.

Here's a tutorial on how to build a project with a "MEN" stack: https://github.com/maslennikov/node-tutorial-men or this one: https://scotch.io/tutorials/build-a-restful-api-using-node-and-express-4

Comments

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.