Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to organize large Node.js projects

Tags:

node.js

What are some good ways to organize large Node.js projects?

For example, an app making use of both express.js and socket.io? This would include both application logical structure as well as filesystem.

Currently, I'm finding myself shoving a ton of code into a single master js file and placing code into a giant global object, and it feels naughty.

like image 798
Thomas Hunter II Avatar asked Mar 22 '12 23:03

Thomas Hunter II


People also ask

Is Node JS good for big projects?

Node Js is not suitable for big computation projects . because it's single threaded .

Is Express good for large projects?

Middleware frameworks, like Express. js, are suitable for small and medium projects. If you are going to develop a large project that will be supported by a large team of developers, Express. js is not the best choice.

Is Node JS still relevant 2021?

js dead? The short answer is “NO.” The long answer is, “NO, it's not dead, and it probably will never die.” Node. js is just as relevant to coding in 2021 and beyond, even if the hype around it has stabilized slightly.


2 Answers

A Beginners Example

I like the originally checked from @david-ellis and you should study it in depth to understand it as it is a good one. However, I would have liked it more simplified for the beginners wanting to see a straight forward example. Here's what I would have liked to have seen someone show me.

Let's give a typical scenario where you are using express and you have a lot of routes listed on your app.js file. Its contents would look something like this:

app.js

// ... startup code omitted above  app.get('/', function(req, res) {   res.render('index', { title : 'home' }); }); app.get('/contactus', function(req, res) {   res.render('contactus', { title : 'contact us' }); }); app.get('/anotherpage', function(req, res) {   res.render('anotherpage', { title : 'another page' }); }); // and so on... 

You can imagine if you have 50 routes, this file can get quite out of hand. It would be nice to remove some of this clutter out of the app.js file.

What you would do is create a "controllers" folder in your app so your structure would now look like this:

app.js /controllers 

Create a file within "/controllers" named "index.js" then put the following code.

/controllers/index.js

module.exports.set = function(app) {    // copy your routes listed in your app.js directly into here } 

Cut and paste your route listings from your "app.js" file and place them into the "/controllers/index.js" file.

On your app.js file, remove your routes and in place of them do the following.

app.js

// remove your routes and replace with this code var controllers = require('./controllers'); controllers.set(app); 

Now if you wanted to have your "/controllers/index.js" file also be split up, let's add one more example so you can see how Node.js really acts like a Russian Doll in how its code can be organized.

Within "/controllers" add one more file "accounts.js" and place the following within it.

/controllers/account.js

module.exports.set = function(app) {     // put more app route listings here } 

Now within your "/controllers/index.js file, put a reference to "account.js"

/controllers/index.js

var account = require('./account.js');  module.exports.set = function(app) {    // your routes here     // let "account.js" set other routes    account.set(app); } 

As you can imagine, you can keep breaking things up into smaller and smaller parts and put more folders within folders and reference with "require" if you like. You can use the same concept for "/lib" or library files. "node_modules" is already doing this.

That is just one of many reasons node.js is very enjoyable to program with.

Manageable Express 4 Routing example

Here's another post I responded to about express 4 routes that relates to this.

Rest with Express.js nested router

like image 154
King Friday Avatar answered Oct 01 '22 02:10

King Friday


I wrote a blog post about this very subject a few days ago, and although the article is in French, I set up a GitHub repo (in English) to show a working example of the structure I use.

Obviously, there is no definitive answer to this question, but it's interesting to see what others are doing, and I am all ears for other opinions on the subject (which was also discussed here, where you can see a summary of what I suggest).

like image 22
mna Avatar answered Oct 01 '22 02:10

mna