Learning Node.js — Part 2
Implementing Express Web Server & Building Endpoints, Nodemon for Automatic Server Updating, Postman to call/view HTTP Services, Joi for Data Validation
What is Express?
Express is a Node.js web application framework². It is fast and lightweight way to build a RESTful backend service. We will use it to build a web server on top of Node.
In your terminal, navigate to your application path, in the terminal type command³: npm i express .
Building an Express Web Server
In your application, create a new file: index.js . Load the Express module as follows:
As you can see, the express() function returns the Express object, that we have now named ‘app’. The app object that we get from Express, comes with the CRUD methods (Get/Post/Put/Delete) that we need in order to build HTTP services.
Implement Express Endpoints
You’ll find below that we will define the different endpoints with methods that come along with our app object. These methods take in two arguments, the path and a callback function (aka: a route handler). We will also need to tell our application/Express to listen on a given port (app.listen()).
In your terminal, type command: node index.js to get your Express server up and running. In your browser navigate to: http://localhost:3000/ , and if setup was a success, you should see the message ‘Hello! Server is running successfully’. If you visit http://localhost:3000/api/musicians, you will see the list of musicians object.
Please remember to restart your server anytime you make a change in any files associated with the server.
Find in-depth documentation on the Express API reference (specifically on request and response properties) here.
Nodemon — Node Monitor
Nodemon is a node package that will listen to changes to our code so that we don’t have to repeatedly stop and start our server each time we change the code affecting it. To install this package, type terminal command: sudo npm i -g nodemon . (Again, sudo is the mac command here) . To run our applications, the terminal command will no longer be node index.js, but instead, will now be nodemon index.js . Nodemon will automatically restart the server every time there are changes.
Explicitly Setting Environment Variable / Port
Our default localhost port is 3000. To change the port and set our environment variable, stop your server in the terminal with command-c , and, as an example, type terminal command: export PORT=5000 . Restart the server, nodemon index.js , and you will see that the dynamic message shows “Listening on port 5000”.
Remaining CRUD Endpoints
Along with our new routes below, you’ll see in our code below, on line 5, we added a piece of middleware that allows us to use/read json from our response body: app.use(express.json()) .
Postman is a Chrome extension that is a platform for API development⁴. It can be used to call/view our HTTP services and to double check that our routes and CRUD actions are behaving as we expect. Automate manual tests and integrate them into your CI/CD [continuous integration and either continuous delivery or continuous deployment] pipeline to ensure that any code changes won’t break the API in production⁴.
This is an example of checking the GET request in postman from a movie genre app I was previously building:
Joi — NPM for Data Validation
In the code above, you can see that we required the Joi module for the application and set it to an object. When in use, if you were to console.log the Joi object in the app.post() route, you would see that Joi returns 4 properties, we are most interested in the error and value props:
REST — Representational State Transfer, Convention for building HTTP services
CRUD — Create, Read, Update, Destroy
HTTP Methods — Get, Post, Put/Patch, Delete
- Node.js Tutorial for Beginners | Programming with Mosh | “https://www.youtube.com/watch?v=TlB_eWDSMt4&t=2349s”
- Express | “https://expressjs.com/”
- Express (npm) | “https://www.npmjs.com/package/express”
- Postman | “https://www.postman.com/”
- Joi | “https://joi.dev/api/?v=17.4.0”