3

In our node js application, all api calls are in server.js. Its a working application, but I'm not happy with every api written in one file i.e. server.js. Many of the examples related to node js application, point to writing server side calls in one file named app.js or server.js. To achieve code modularity, I'm planning to separate the api's in different files like employee.js (employee related data transaction), library.js (library related data transaction) and so on to respective modules. Not sure whether this is the right approach as node.js server needs to be launched on application invoke. So, if the api's are distributed to different files then need to check how to launch the node js server in one shot.

Also, we have integrated node-webkit with the application.

Need suggestions in this regard.

1
  • It would be helpful to see how you are implementing your code. Commented Jul 15, 2015 at 6:31

2 Answers 2

2

You can use the express middlewares and the default module system in node.js to achieve this.It is a good practice to keep the code modular and separated. One common way to do this is add separate folders for all different routes/controllers(employee in your case) something like this

app.js

const express = require('express')
const app = express()

const employeeController = require('./controller/Employee');
const managerController = require('./controller/Manager');

app.use('/employee', employeeController);
app.use('/manager', managerController);


app.listen(3000, () => console.log('App listening on port 3000!'))

./controller/Employee/index.js

const express = require('express')
const router = express.Router()

router.get('/list', (req, res) => {
  res.send('Employee list')
});
router.get('/view', (req, res) => {
  res.send('Employee view')
});

module.exports = router

./controller/Manager/index.js

const express = require('express')
const router = express.Router()

router.get('/list', (req, res) => {
  res.send('Manager list')
});
router.get('/view', (req, res) => {
  res.send('Manager view')
});

module.exports = router

you can even get a readymade boilerplate MVC code structure by using express-generator here https://expressjs.com/en/starter/generator.html .However i reccommed to structure your project based on your needs.

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

Comments

1

One of greatest things in node.js is its module system (you can read about it in docs: https://nodejs.org/api/modules.html), which gives you like every option of granularity you would like to take, from one-thousand-lines files to files that export just one constant.

So just split code in files, module.exports = thatthingyouwanttoexport and then require('./it') back in app.js. If you do it at top-level, not inside any callback in app.js, then it would be done right at the start of your application.

Few catches:

  • required modules are cached, so requiring it twice doesn't execute your files twice, it can be useful (e.g. for singletons), but it's still thing to note;
  • you export only things that you're exporting, so no globals

One useful pattern is to export function, which takes arguments and does initialization when called, like this:

//accum.js
var start = 0
module.exports = function initaccum(step) {
    return function accum() {
        start += step;
        return start;
    }
}

//app.js
var accum = require('./accum.js')(1);
var http = require('http')
http.createServer(function(req, res) {
    res.end(accum());
}).listen(8080);

Hope this helps.

1 Comment

Yup, the method in which I had implemented is the same. But, forgot to answer in this post. Thanks for sharing your thoughts !!!. This will definitely be helpful to others looking for the same question.

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.