- What will we create?
- Setting up node and express
We are creating a REST API for a simple blog post that will serve blog posts and author information. You can implement this API with any front-end framework of your choice.
The REST API will have three layers.
- Schema: Describes the structure of the database.
- Service Layer: Describes business logic. How to access the database? How to process data from a database?
- Controller Layer: Handles HTTP requests and responses. Calls multiple services to complete the requested work.
Such a layered architecture supports Separation of concern where the layer has its responsibility. Such independence helps in the easy maintenance of the project.
We are using the following framework and tech in this project:
- Express.js: A Node.js framework that provides features to help build web applications.
- dotenv: Helps to manage the app's configuration and environment.
- nodemon: A dev tool that will monitor changes in our app and restarts the app for us.
- Postman: A dev tool for testing the API app and its working. We will use this tool after project completion.
- Create a project folder, navigate inside the folder, and open a terminal. It will be our project root.
- Initialize the node with the command:
npm initor use the command
npm init -yto skip all questions asked during initialization.
- It will create a
package.jsonfile in our project root. This file holds various metadata relevant to the project. This file will give information to npm that allows it to identify and handle the project's dependencies and scripts.
Install dependencies with the command:
npm install -save mongoose express dotenv
Install dev dependencies with the command:
npm install --save-dev nodemon
Create a file
server.js. Then make
server.jsour app's entry point. Inside
package.json, make the following changes:
Add the following script in
server.jsunder the monitoring of nodemon.
package.jsonshould look something like this:
If you are using git, don't forget your
.gitignorefile. Here is a
.gitignoretemplate you can use for the node project.
.envfile in the project root with the following config. This config will be accessible through the node process environment. It is where you keep all your private keys and settings. So, don't expose it publicly.
In project root, create an
srcfolder. It is where we will keep all our source code.
srcfolder, create a
config.jsfile with the following code:
In this code snippet, we are creating an express app and listening to port 5000 for our request.
The file structure looks like this:
Open terminal in our project root and run command
npm run server
Just a tip, during our entire project series, run the server app to get immediate feedback from the console. It will be easier to detect any bug earlier in our project development.
Now open a browser and type this URL:
localhost:5000is our hostname, and
/api/v1/is an end-point that handles our get request.
You will get a JSON response from our server.
- We initiated our project, did some configuration, and ran our server app.
- In the next part, we will connect to our MongoDB database and create some schema and model for our database.
- The link to the project repo is available here.