DEV Community

loading...
Cover image for What the node.js project structure should look like?

What the node.js project structure should look like?

kalpitrathore profile image Kalpit Rathore ・3 min read

Introduction

In this article, we will going to talk about organizing node.js project in a structured way.

Alt Text

Organized project structure reduces bugs & delicacy, Increases the stability & scalability of the code.

The Structure

|--app.js
|--api-routes
|--config
|--data
|--jobs
|--loaders
|--node_modules
|--public
|--secrets
|--services
|--views
|--package-lock.json
|--package.json
Enter fullscreen mode Exit fullscreen mode

1. app.js

app.js is the entry point or heart of the node.js application

//app.js
const express = require('express');
const app     = express();

require('./loaders/network-information.js');
require('./api-routes/home.js')(app);
require('./jobs/say-hello.js');
var config = require('./config/config.js');

app.set('view engine', 'ejs');
app.use(express.static('public'))

app.listen(config.port, () => {
  console.log(`App listening at http://localhost:${config.port}`);
})
Enter fullscreen mode Exit fullscreen mode

2. api-routes

api-routes contain URL endpoints, which help users to interact with the application.

|--api-routes
   |--home.js
Enter fullscreen mode Exit fullscreen mode
//home.js
var analytics = require('../services/analytics.js');
module.exports = function(app, network_information){    
    app.get('/', function(req, res){
        res.render('home.ejs', {key: "You have visited this page "+analytics.visitCounter()+" times"});
    });    
}
Enter fullscreen mode Exit fullscreen mode

3. config

This folder contains files that are used to configure the parameters & initial settings for application. They can be overridden & extended by Environment Variables, Command Line Parameters, or External Sources

|--config
   |--config.js
Enter fullscreen mode Exit fullscreen mode
//config.js
module.exports = {    
    port: process.env.PORT || 80
};
Enter fullscreen mode Exit fullscreen mode

4. data

Here you can store your small data or can use it as a local database
Note: Don't store any sensitive or confidential data here, Always use secure database for that kind of data.

|--data
   |--users.json
Enter fullscreen mode Exit fullscreen mode
//users.json
[
    {
        "name": "Mario Curtis",
        "city": "San Francisco",
        "contact number": "4949494949780",
        "type": "Admin"        
    }
]
Enter fullscreen mode Exit fullscreen mode

5. Jobs

jobs folder contains scripts that will execute themselves automatically at a particular time
Just like, Here we have created a script which prints "Hello" every hour a day.

|--jobs
  |--say-hello.js
Enter fullscreen mode Exit fullscreen mode
//say-hello.js
var schedule = require('node-schedule');     

schedule.scheduleJob('00 * * * *', function(){
    console.log('Hello World, This scheduler will say "Hello" every hour to you');
});
Enter fullscreen mode Exit fullscreen mode

6. loaders

loaders contains the type of scripts that will execute at the time server starts.
Just like, Here we are fetching user's IP Address whenever server starts

|--loaders
   |--network-information.js
Enter fullscreen mode Exit fullscreen mode
//network-information.js
const http = require('http');

http.get('http://api.ipify.org?format=json', (resp) => {
  let data = '';

  resp.on('data', (chunk) => {
    data += chunk;    
  });

  resp.on('end', () => {    
    console.log("Your IP address is "+JSON.parse(data).ip)
  });

}).on("error", (err) => {
  console.log("Error: " + err.message);
});
Enter fullscreen mode Exit fullscreen mode

7. public

public folder contains all the files that have to be served "statically" (JS, CSS, Image File).

|--public
   |--css
   |  |--home.css
   |--js
   |  |--home.js
Enter fullscreen mode Exit fullscreen mode
//home.css
body{
    background-color: #f3f7fa;
}

div{
    text-align: center;
    padding-top: 200px;    
}
Enter fullscreen mode Exit fullscreen mode
console.log('Hello from javascript');
Enter fullscreen mode Exit fullscreen mode

8. secrets

You can store all your secrets related to web application such as API-KEY, AUTHENTICATION-KEY, etc.

|--secrets
   |--keys.json
Enter fullscreen mode Exit fullscreen mode
//keys.json
{
    "api-key": "HUH121NVYTB091BHHBCR121DR"
}
Enter fullscreen mode Exit fullscreen mode

9. services

services contains set of modules which performs a specific task. They can be used over & over again.

|--services
   |--anaytics.json
Enter fullscreen mode Exit fullscreen mode
//analytics.json
var i = 0;
function visitCounter()
{
    i = i + 1;
    return i;
}

module.exports = {
    visitCounter: visitCounter
}
Enter fullscreen mode Exit fullscreen mode

10. views

It contains HTML Template files, Which later rendered "dynamically" by Templating Engine & serve HTML response to user.
Here we are using EJS templating engine, You can use any templating engine(pug, ejs) as per your convenience.

|--views
   |--home.ejs
Enter fullscreen mode Exit fullscreen mode
//home.ejs
<html>
    <head>
        <title>Node.js Project Structure</title>
        <link rel="stylesheet" href="/css/home.css"/>
    </head>
    <body>
        <div><%= key %></div>
    </body>
    <script src="/js/home.js"></script>
</html>
Enter fullscreen mode Exit fullscreen mode

Conclusion

Organized folder structure reduces the code complexity & increases the scalability of application.

You can find whole project on GitHub.

For more update, Follow me on Twitter or GitHub.

Discussion (4)

pic
Editor guide
Collapse
beginnercoder17 profile image
BeginnerCoder17

what is Node.JS im new to JS and was wondering what it is

Collapse
rahuldahal profile image
Rahul Dahal

Node JS is a run-time engine that enables JavaScript code to run outside of the browser. This enables developers to create a server, desktop, and mobile applications; all from a single language. JAVASCRIPT.

Collapse
kalpitrathore profile image
Kalpit Rathore Author

In simple language, Node.js is an open-source JavaScript tool use to create a server environment. With Node.js we can execute JavaScript code outside a web browser.

Collapse
beginnercoder17 profile image
BeginnerCoder17

Thanks a lot!