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
-
.env
file contains environment variables and you must add this file because it wonβt be pushed due to.gitigonore
file. We will useAPI_PORT
variable, then you need to add it just likeAPI_PORT=20000
where20000
is your desired port. -
.eslintrc.json
is used to follow some basic coding rules. You can use it or just ignore it. -
.gitignore
to defined everything you donβt want to push to git repository. Here is so import to add node_modules. -
README.md
just to show some information about the project. -
package-lock.json
keeps the packages dependencies tree tracked. -
package.json
has every needed dependency for this project. -
src
folder where will leave our core code.-
common
folder to define common functions used along whole project. -
index.js
that contains main API configuration. -
middlewares
as the name says, contains every API middleware. In this case we only will use just one for error handling. -
routes
folder where the API routing is defined. Here you can find every endpoint of the API.-
index.js
works like a routes mixer, just that. -
public.js
contains 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/:lang
where 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)