Node.js API development powered by Express
Note: this post is not recomended for SSR or SR developers due to its simplicity.
Let’s start with a brief conceptual review 💪
First of all let’s talk a little bit about client-server architecture. This is a strongly used model to define a way to communicate one or more parts that request some services (clients) and one or more service providers (servers). First of them could be a website or in fact a mobile application, second of them could be a SOAP webservice, Rest API, among others.
We are going to explain one specifical server service called Rest API, in this opportunity applied with Node.js
Express at a glance 👀
This amazing framework will help you build a robust Node.js Rest API. It’s important to know that there are other options to take into account like Sails, Meteor, Happi, among others. In this case we will use Express which is the most used one but it doesn’t limit you to use other one. You can read the whole Express documentation here
Requirements 🛠
- Node.js.
-
Nodemon (Optional), used to reload the Rest API when a change is introduced on your code. You can install it with npm globally:
npm install -g nodemon, also you can use it with npx.
Installation ⚙️
Once the repository is cloned you should install npm packages to be able to run it. To achieve this, just open a terminal on root directory and run the following command:
npm install
Let’s start with some code 💻
First of all, I will present the folder structure I chose. That’s so simply and I didn’t include some concepts like services or database acceses.
.
├── .env
├── .eslintrc.json
├── .gitignore
├── README.md
├── package-lock.json
├── package.json
└── src
├── common
│ └── error.js
├── index.js
├── middlewares
│ └── errors.js
└── routes
├── index.js
└── public.js
-
.envfile contains environment variables and you must add this file because it won’t be pushed due to.gitigonorefile. We will useAPI_PORTvariable, then you need to add it just likeAPI_PORT=20000where20000is your desired port. -
.eslintrc.jsonis used to follow some basic coding rules. You can use it or just ignore it. -
.gitignoreto defined everything you don’t want to push to git repository. Here is so import to add node_modules. -
README.mdjust to show some information about the project. -
package-lock.jsonkeeps the packages dependencies tree tracked. -
package.jsonhas every needed dependency for this project. -
srcfolder where will leave our core code.-
commonfolder to define common functions used along whole project. -
index.jsthat contains main API configuration. -
middlewaresas the name says, contains every API middleware. In this case we only will use just one for error handling. -
routesfolder where the API routing is defined. Here you can find every endpoint of the API.-
index.jsworks like a routes mixer, just that. -
public.jscontains the unique endpoint we have.
-
-
Running ▶️
Once npm packages are installed you will be able to run the API. If you have installed nodemon you can run it with:
nodemon ./src/index.js
Otherwise:
node ./src/index.js
After this you should show a message like this on your terminal:
Node.js API listening on port: {Your port defined on .env}
At this point, you can go to your favourite browser, put http://localhost:{yourDesiderPort}/api/v1/en and check that the response will be:
{
"success": true,
"message": "Node.js API - Hello world"
}
Available endpoints ✔️
-
GET /api/v1/:langwhere lang is related to a language code. The possible values are['en','es', 'it', 'fr']. This endpoint will return a specific message depending on the language code sent.
Github repository link
You can access to the full code in this Github link
Top comments (0)