Getting Started template project for NodeJS Express JS

Abhishek Rathore
5 min readMar 3, 2021

Express JS is a widely used Node JS framework for creating REST API application, but there is no quick start project template available to quickly learn and start creating a professional REST API project.

So this is my attempt to create a hello world project which will not only give a quick start into Node JS Rest API development but a quick guide for how to create a clean project structure using Express JS.

Let’s first outline the project structure which we would be implementing before diving into Node and Express JS :

Now let’s create project and install dependencies

1. Install Node (Go to https://nodejs.org/en/download/ and install the latest)
2. Create a folder with project name in our case it is HelloWorldNodeJS
3. Now open it in Visual Studio Code and open Terminal in code
4. In Terminal type and follow the instruction or just press enter: npm init
5. Once npm init is complete you will see package.json created in the folder, this contains all the dependencies and project configuration
6. Next we will install all the dependencies or packages we need to start creating our application :

- npm install express // For installing expressJS
- npm install –save-dev nodemon //nodemon auto restarts the server once you make any change in code and we have registered it as dev dependency so will not be part of main package

- Npm install mongodb
- Npm install mongoose (provides api for schema based modeling solution to model data)

Now package.json created in the porject folder should look like below

We will be using typescript for this project so let’s install it

npm install typescript
npm install @types/node --save-dev
npm install express @types/express

Now let’s create some folders and empty files in VS code as below so that we have project structure in place

Config.json

We will add all project level configuration setting in this file

port : Application will listen to the mentioned port for any request
dbUrl: We will be connecting to Mongo DB for storingand reading data in this tutorial. You can also in Mongo DB compass which provides a UI for querying the database. HelloDB is the name of database which we will be connecting to.

Startup.ts

We will be using Startup.ts to add middleware’s (register controllers ,express framework json and code for start server on port).

All middlewares should be registered in setup method. We are registering express first using this.app.use method. Express provides framework for web application development, we will need express to create routes in controller. Next we are registering messageController which will contain routes for HTTP GET/POST/DELETE endpoints

Notice run method, this will be called from app.ts to start the application on specified port.

App.ts

App.ts is the entry point of our application as we have specified in package.json

In App.ts we are importing Startup object, initializing and starting application on port mentioned in config.json

To read config.json we used NodeJS require function
var CONFIG = require(‘./config.json’)

Model/Message.ts

Here we define schema for MongoDB database using MongooseJS library and we are exporting mongoose.model object which will be used to connect to MongoDB database. For this tutorial we will just save a message and date on which message was posted in database.

Biz/MessageBiz.ts

In constructor we are using connect and connection object from mongoose library to initialize mongodb connection. Once connection is made messageModel from Messages.ts could be use to find or save data to database.

Controller/Message.ts

Express framework provides Router object which could be used to create HTTP GET/POST/DELETE endpoints.

To handle request of the type http://……/message pass ‘/’ as path parameter in router.get method.

router.get(‘/’, async(req,res)=>{})

If we want to accept any query string parameter in http request then we can pass parameter name in path as ‘/:id’

router.get(‘/:id’, async(req,res)=>{

For creating HTTP post endpoint we can use router.post method

router.post(‘/’, async(req,res)=>{})

Now Lets run the application using npm start on VS code terminal and make a post request using Postmen

We can verify that data has been saved in MongoDb by using MongoDB compass application

Filter

Now there is one more piece pending which is mostly used in a real world application and is a preprocessor or a filter which could be used to log an incoming request or validate incoming request etc or validate/authenticate incoming request.

To keep it simple lets just log any incoming request and outing response status code and URL. Middleware method accepts request, response and next object which calls next middleware in the stack

We have to return next() explicitly so that next middle could be called in request processing pipeline.

Next register this middleware in Startup.ts

this.app.use(requestLoggerMiddleWare);

Now all request made to the message endpoint will be logged

GitHub Link https://github.com/arathore4/NodeExpressProjectTemplate

I hope this article gives a quick start to NodeJS and Express JS application development

Thanks

--

--