Express JS - basic
What is Express.js ?
At the time of writing, Express (v4.18.3) is Fast, unopinionated, minimalist web framework for Node.js
Bolierplate code (at the minimum)
-
Create a folder and initialize a node project (with default values)
-
Install the
express
package and middlewarebody-parser
-
Create a file called
index.js
and copy the following code -
Run the server using following command
-
The server should be started at
http://localhost:3000
. Upon opening the link, “Hello, Express!” will be served
This is a very minum setup required to start a Express server.
Body Parser middleware
In the above code, body-parser
middleware is installed. This is usually required, in case developing an JSON-based API.
So, we can now extend the server code like below, to get users and set (update) a particular user.
Test the above code
-
To get all users, go to the location
http://localhost:3000/users
-
To post (or update) an user, you can simply use any REST Client extensions or if
cURL
is available, then the following code updates user2 -
Upon successfull execution, the following result should be shown.