1.

How to install Express.js?

Answer»

Since Express.js is a framework for NODE.js, the basic requirement is to have Node.js INSTALLED on the system. The installation of Node.js can be done in MANY DIFFERENT ways, depending on the operating system. The best way is to go to the Node.js official site https://Node.js.org/en/ and install it. 

Node.js comes with NPM install and it is required, because Express.js is an external node package, which needs to be added in a Node application. 

To show how that works, we will create a Node.js application and add Express.js to it. First, create a folder and then make changes to it.

Now, give the command ‘npm init’ to initialize a node app. We can leave most of the questions as default by pressing enter. But we'll give the entry point as server.js. We’ll also assign an author. And finally, enter ‘yes’ for the last question. 

This creates a package.json file will in the same directory, and we can install npm packages now.   Then, we’ll add express in our Node.js application by giving the command npm i express from the same directory in the terminal.  

Next, we’ll create a file server.js in the root directory and add the below code in it. Here, we are first importing express and using it in an app. After that, we’re also declaring a port and MAKING it equal to 3000. Next, we have a GET request to /, where we will send Hello Express to the client. 

Finally, we are listening at port 3000 for incoming requests. 

Now, we will start the express app by running the command node server.js from the terminal.

Then we will go to http://localhost:3000/ and see the text Hello Express in the browser. 



Discussion

No Comment Found