Learning Node.js — Part 2

https://codewithmosh.com/

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.

Install Express

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

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⁴.

You can find the Chrome Postman extension download here, but Postman now has moved to it’s own app and you can download it here.

This is an example of checking the GET request in postman from a movie genre app I was previously building:

Postman example from previous project.

Joi — NPM for Data Validation

Joi is a node package for defining schemas and then validating that schema’s data for JavaScript⁵. To install Joi, type terminal command: npm i joi . (((IMPORTANT: Mosh uses npm i joi@13.1.0 , this is necessary for the code above to perform correctly w/o errors)))

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:

SIDE NOTES:

REST — Representational State Transfer, Convention for building HTTP services

CRUD — Create, Read, Update, Destroy

HTTP Methods — Get, Post, Put/Patch, Delete

  1. Node.js Tutorial for Beginners | Programming with Mosh | “https://www.youtube.com/watch?v=TlB_eWDSMt4&t=2349s
  2. Express | “https://expressjs.com/
  3. Express (npm) | “https://www.npmjs.com/package/express
  4. Postman | “https://www.postman.com/
  5. Joi | “https://joi.dev/api/?v=17.4.0

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Osha Groetz

Osha Groetz

93 Followers

Software Engineer. A new addict of ReactJS & Javascript, CSS & APIs, with a little dabbling in Ruby, Ruby On Rails…