Base Express Application Setup – Node.js API with TDD Tutorial

Before we begin any coding for the RESTful API, we need a base application. To set it up, we need to install the Node, Express and other dependent modules as well and create the basic application structure.

You can refer the previous post on setting up the development environment which includes Node.js installation as well.

Install Express.js

For our API development in this tutorial, we will be using the Express web framework for Node.js. This one is a framework of choice for many API developers as it has various HTTP methods and middleware utilities needed for full-blown API development. 

The express module can be installed via npm commands as below. If we try this route, we will have to create the basic application by ourselves from scratch right from the package.json via npm init command and other required files and directories.

To install the express module, follow the below steps.

Create a directory for our app and get inside the newly created directory express-service

mkdir customer-service
cd customer-service

Once changed the working directory, run npm initialization command as below. This command execution will create the package.json file with initial default values.

After running this command, it will ask for values to be entered for many attributes of the package.json. Either accept the default value shown for each attribute or enter your choices to complete the initialization.

npm init

Now is the time to install the express module within your app. With below command, the express module will be added to the package.json as a dependency.  Here –save is very important as it’s the command to let the express module to be added to the package.json file.

npm install express --save

Next steps will be to create the required files and directories for the base application manually. Although there is a simple step to accomplish this task. The next section will be explaining the quickest way to get the base app up and running.

Install Express Generator

Instead of creating the express app manually from scratch, we could use the Express Application Generator tool. This tool will install the express command line tool which you can use to generate the express app skeleton quickly with a single command. 

Run the below command to install the Express Generator package. You might need to run this command as the administrator of the machine. This command will install the express generator module globally so that we can execute the express command anywhere to create the express app.

npm install express-generator -g

Generate Express Application

Once the module has been installed, run the below command to generate the initial express application, and it will come with all the base application features for us to begin. 

express customer-service

In this command, the parameter ‘customer-service ‘ is the app name, and it will create the directory with the same name under the current working directory. In the new app directory, all the express app related files and directories will be automatically created.

Here is the screenshot of the express application created by the express-generator tool.

Base Express Application Setup - Express Generator Files & Folders View
Base Express Application Setup – Express Generator Files & Folders View

As the output of the above command says, go inside the new app directory and run the npm install command to install all the dependencies to be downloaded locally under the node_modules directory. This directory will hold all the packages in the dependencies list of the package.json file.

cd customer-service
npm install

After all of the dependent packages are downloaded, run the below command to start the node server for this app.

npm start

Now, open a browser and enter the link http://localhost:3000 to verify and confirm the app is running. If it’s running, you will be able to view the below page rendered on the browser.

Access Generated Express Application
Accessing Generated Express Application

You can checkout the below link to the source code for this step in GitHub.

GitHub – Step 01 – Generated Express Application

Please check out the index page for this Node.js RESTful API development with TDD approach tutorial with all the posts in sequence in one place.

This blog post is an excerpt from the book Building Node.js REST API with TDD approach. Please check out the link for more information.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.