Boilerplate structure for Node.js + Express.js

Boilerplate structure for Node.js + Express.js

1. Starting the project

Obviously, the first step is installing Node.js and Yarn on your local OS. The following links point to both official documentations regarding their installations:

After installing them, start your project (which we will call project) by running:

$ yarn init -y
Obs: the -y flag makes Yarn answer yes on every question in order to create the package.json file.

2. Installing required packages

There are a couple of packages which I recommend using in almost every project. I will list them below, already formatted in json, in order to be used in your package.json file:

"dependencies"

: {

"bcrypt"

:

"

^4.0.1

"

,

"body-parser"

:

"

^1.19.0

"

,

"cors"

:

"

^2.8.5

"

,

"dotenv"

:

"

^8.2.0

"

,

"express"

:

"

^4.17.1

"

},

"devDependencies"

: {

"nodemon"

:

"

^2.0.2

"

}

Besides these, you will also need and ORM to query into your database of choice. For mongoDB, I’d recommend using “mongoose”, and for any other database “sequelize” will do the trick.

After adding them to your json file, run yarn to install them.

3. Creating the folder structure

Our app custom files will be stored inside a folder called “src”, which will contain our config files in the root and other folder containing the logic of our application.

We will have, then, something like this:

project
|__ node_modules
|__ src
|  |__ database
|  |  |__ index.js
|  |__ modules
|  |__ app.js
|  |__ routes.js
|  |__ server.js
|__ package.json
|__ yarn.lock

4. Instantiating the Express server

On our app.js file, let’s instantiate our Express server, requiring it from our node_modules. Let’s also create some middlewares, and connect our application to the database config and routes file.

const

express

=

require

(

'express'

)

;

const

cors

=

require

(

'cors'

)

;

const

bodyParser

=

require

(

'body-parser'

)

;

require

(

'./database/index'

)

;

class

AppController

{

constructor

(

)

{

this

.

express

=

express

(

)

;

this

.

middlewares

(

)

;

this

.

routes

(

)

;

}

middlewares

(

)

{

this

.

express

.

use

(

express

.

json

(

)

)

;

this

.

express

.

use

(

cors

(

)

)

;

this

.

express

.

use

(

bodyParser

.

json

(

)

)

;

}

routes

(

)

{

this

.

express

.

use

(

require

(

'./routes'

)

)

;

}

}

module

.

exports

=

new

AppController

(

)

.

express

After that, it is time to create our first route and start the server.

On your routes.js, add the following:

const

routes

=

require

(

'express'

)

.

Router

(

)

;

routes

.

get

(

'/'

,

(

req

,

res

)

=>

{

res

.

send

(

"Hello, World!"

)

;

}

)

;

module

.

exports

=

routes

;

And on your server.js:

const

app

=

require

(

'./app'

)

;

app

.

listen

(

3000

,

(

)

=>

{

console

.

log

(

'App listening on port 3000'

)

;

}

)

;

5. Creating the script to run the server

On a development environment, we will be using nodemon to start up our server. To do that, just add the script to run the application in your package.json file:

"script"

: {

"dev"

:

"

nodemon run src/server.js

"

}

And that is it!

With that done, simply run yarn dev, and then your server will be running on port 3000. You should be able to see “App listening on port 3000” in your console.