Node.js tutorial: How to use Express.js on MacOs
In the last tutorial we showed here how to create URL-based requests using an if / else structure.
Know how you can use Express.js to perform the same operations in a much simpler way.
Express.js is a framework for Node.js that allows the development of Web applications to be very simple. The installation of Express.js in the programming environment must be done through NPM. NPM is a package manager for javascript.
How to install Express.js?
To install Express.js, just run this command inside the work directory:
npm install express -save
Now considering the last example, let’s refactor the code using the features of express. The final code will be something like this:
After you create this index.js file, the compiler must run by executing the command:
node index.js
To test, you must open the browser and enter the URL (http://localhost:8080/)
To access the categories we created, we type one of these URL, (http://localhost:8080/laptops), (http://localhost:8080/smartphones), (http://localhost:8080/tablets).
This is how you use Express.js.