ExpreesJS Introduction#
What Is The ExpressJs?#
-
Express.js
is a small framework that works on top of NodeJs web server functionality to simplify its APIs and add helpful new features. It makes it easier to organize your application’s functionality withmiddleware
androuting
. It adds helpful utilities to NodeJs HTTP objects and facilitates the rendering of dynamic HTTP objects. -
More information (https://www.geeksforgeeks.org/introduction-to-express/?ref=lbp)
Why ExpressJs?#
- Develops NodeJs web applications quickly and easily.
- It’s simple to set up and personalise.
- Allows you to define application routes using HTTP methods and URLs.
- Includes a number of middleware modules that can be used to execute additional requests and responses activities.
- Simple to interface with a variety of template engines, including Jade, Vash, and EJS.
-
Allows you to specify a middleware for handling errors.
-
More information (https://www.geeksforgeeks.org/introduction-to-express/?ref=lbp)
What Is The Middleware?#
-
Middleware
in a NodeJs application refers to functions or modules that are executed in between the processing of an incoming request and the generation of a response. Middleware functions have access to the request and response objects, as well as thenext
function, which is used to pass control to the next middleware function in the chain. -
Middleware
functions can be used for a variety of purposes, such as logging, authentication, data validation, error handling, and more. They provide a way to modularize and organize the functionality of an application by breaking it into smaller, reusable components.
Install Express#
- Now, let's create a project
express-demo
then runnpm init
to initializepackage.json
. After that let's installExpress
dependency to project by using the command below.
1 |
|
Building First Web Server#
- Okay, so let's create a file
index.js
and put the example code below.
index.js | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
- Okay, as you can see, we will use
require('express')
to import express module and we will create an instance of the Express application by calling theexpress()
function and this instance will be stored in the constantapp
. - Next, from the
app
we can use supported functions ofexpress
to create Apis and start the server which will listen on given port (Ex: 3000). If the server is started successfully then a log will be printed out. - Finally, let's start the
express-demo
and go to browser with 2 exported apis above, we can see the result as below.
Nodemon#
Nodemon
is a utility tool for NodeJs developers that helps with development workflow by automatically restarting the NodeJs application whenever changes are made to the source code files. It stands for "Node Monitor," and its primary purpose is to save developers from manually stopping and restarting the NodeJs application each time they modify the code.- To install
Nodemon
, let's use the command below.
1 |
|
- Now, instead of starting our node application by using
node index.js
. We will start it withnodemon
.
1 |
|
1 2 3 4 5 6 7 |
|
- Now, if we make any change, then the server will be restarted automatically.
1 2 3 4 5 6 7 8 9 10 |
|
Environment Variables#
- As we can see, in our code the server port is hard code with port 3000, it is worked on our machine but maybe it will not work on other machine. We should not force every machine have to spend port 3000 for our application. So to fix it, we can use an
environment variable
. - An environment variable is basically a variable that is part of the environment in which a process runs. Its value is set outside this application.
- To read a value from the environment with nodejs we can use the syntax below.
1 |
|
index.js | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
-
As we can see, we will read the value from the environment variable name
PORT
and if this environment variable doesn't exist then we will get the value 3000 for the port. -
Now, to set an environment variable name
PORT
to our machine we can use the command below.
1 |
|
1 |
|
1 2 3 4 5 6 7 8 |
|
Route Parameters#
- In Apis, we usually see there are some parameters on the
path
. So firstly, to define a parameter in theapi
we can use syntax:<parameterName>
1 2 3 |
|
1 2 3 |
|
- The example above show how do we define a parameter
id
in the api. - Next, to read a parameter value that we defined about, we will use
req.params.<parameterName>
.
1 2 3 |
|
- Let's see the example below to read a parameter from the path with
express
.
index.js | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
- With multiple parameters we also have the same way to define and get. Let's see the example below.
index.js | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
- Okay, So we use
route parameters
for essential or required values whereas we usequery
string parameters for anything that is optional. - So if we are going to use
query
string instead ofparam
to provide some additional data to back end services. We will use thereq.query.<QueryName>
.
1 2 3 4 |
|
1 2 3 4 |
|
index.js | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
Handling HTTP Get Requests#
- Now, let's create some mock data and add them into the response for GET Apis that we created.
index.js | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
- In the
/api/courses/:id
we will find the item in the mock array following the id, if there the item is not there then an error 404 will be response with a message.
Handling HTTP Post Requests#
- Now, let's create a POST
/api/courses
Api to create a Course object and push to thecourses
array as below.
index.js | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
|
-
Okay to get the request body json with express we can use
req.body
and to get the specific field in the request body we can usereq.body.<fieldName>
. However, by default we can't do that because thereq.body
is the an object with typeReqBody
and we need to use a Middlewareexpress.json()
to parse thereq.body
to json. Let's see the example below. -
Firstly, we will tell Express to use the middleware
express.json()
. Theexpress.json()
is a built-in middleware function in the Express. Middleware functions in Express are functions that can process incoming HTTP requests before they reach the route handlers.express.json()
middleware specifically parses incoming JSON payloads from HTTP requests and makes the parsed data available on thereq.body
property. It is designed to handle JSON data submitted in the request body, which is commonly used in API endpoints where clients send data in JSON format.
1 2 3 4 5 6 7 |
|
- Now, we can get the specific field from
req.body
json.
index.js | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
- Okay, let's use postman and call POST
/api/courses
with request body as below. Then we will receive a successful response.
1 2 3 |
|
- Now, let call api GET
/api/courses
. Then we can see the new course had been added into the mock courses array.
Input Validation With Joi#
- Okay, when we work with request body of POST Apis we may need to do some validations on fields of request body. In nodejs there is a powerful library can support us which is
Joi
. Joi
is a popular and powerful library for input validation and data schema validation in NodeJs applications. It is commonly used with Express to validate and sanitize data received from clients in API requests or any other input data that needs validation.- With
Joi
, we can define a schema for the expected data and validate incoming data against that schema. It allows us to ensure that the data conforms to specific rules, such as required fields, data types, length, format, and more. By validating input data, we can prevent invalid or malicious data from being processed by your application, enhancing security and reliability. - Now, to using
Joi
in our node application we will need to install it by using the command below.
1 |
|
- After that, we will import the
Joi
object into ourindex.js
for using.
index.js | |
---|---|
1 2 3 4 5 |
|
- Now, let's use
joi
to validate the request body in the POST Api as below.
index.js | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
|
- Okay we have
const schema = Joi.object({ name: Joi.string().min(3).required() });
. This is aJoi
schema definition. It defines the validation rules for the incoming data in the request body. In this case, it specifies that thename
property must be a string, have a minimum length of 3 characters, and be required (i.e., it cannot be empty). - Next we have
const result = schema.validate(req.body);
: Here, the incoming data in the request body (req.body
) is validated against the defined schema using thevalidate
method of theJoi
schema object. Theresult
variable will hold the validation result, which includes anerror
property if the validation fails. - Okay now, let's use postman and test with invalid data as below.
- The failed HTTP status and error details will be through if the request body doesn't match with the the validation rules.
Handling HTTP Put Requests#
- Next, we will continue to create a new PUT Api
/api/courses/:id
for updating a course in the mock array. In this Api we will do some validations.- Firstly, we will check the course id existed in the mock array or not.
- Then we will check the request body is valid or not.
- Okay, let's see the example code below.
index.js | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
|
-
As you can see, we will refactor the code a little bit with function
validateCourse
, we will use it for POST and PUT Apis. For the result of functionvalidateCourse
we are using the syntax{ error }
it means in the returned object we only get theerror
field. -
Okay now, let's use postman to test this api.
- We will get error if the request body is not correct or the id is not found.
- Then if everything is correct then we can see the course with id is updated.
Handling HTTP Delete Requests#
- Next, we will continue to create a new DELETE Api
/api/courses/:id
for deleting a course in the mock array. In this Api we just check the course id existed in the mock array or not. - Okay, let's see the example code below.
index.js | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
|
- As we can see the delete Api is very simple, we just do the check and then delete the item in the array.
-
In the example code above, we also did some bug fixes at validation steps, we will add the
return
error right at these steps to make sure if the validation is failed then the code will not continue to run to the end of the Api function. -
Now, let's use postmand and test the delete api.
- We will get error if the request body is not correct or the id is not found.
- If the id exists in the mock array then we can see the successful result as below.