Saturday, March 21, 2020

Creating First Express Program

ExpressJS – Environment

In this session, we will learn how to start developing and using the Express Framework. To start with, you should have the Node and the npm (node package manager) installed. If you don’t already have these, go to the Node setup to install node on your local system. Confirm that node and npm are installed by running the following commands in your terminal.






Now we have setup node and npm set up, let us understand what npm is and how to use it.

Node Package Manager(npm)

npm is the package manager for node. The npm Registry is a public collection of packages of open-source code for Node.js, front-end web apps, mobile apps, robots, routers, and countless other needs of the JavaScript community. npm allows us to access all these packages and install them locally. You can browse through the list of packages available on npm at npmJS.
Whenever we create a project using npm, we need to provide a package.json file, which has all the details about our project. npm makes it easy for us to set up this file. Let us set up our development project.
Step 1 − Start your terminal/cmd, create a new folder named ExpressJS and cd (create directory) into it −

Step 2 − Now to create the package.json file using npm, use the following code.
npm init
It will ask you for the following information. 
Just keep pressing enter, and enter your name at the “author name” field.
Step 3 − Now we have our package.json file set up, we will further install Express. To install Express and add it to our package.json file, use the following command −
npm install --save express
To confirm that Express has installed correctly, run the following code.
dir node_modules 
This is all we need to start development using the Express framework.
Step 3 − Now create server.js file in the ExpressJS folder:

Hello world Example

Following is a very basic Express app which starts a server and listens on port 3000 for connection. This app responds with Hello World! for requests to the homepage. For every other path, it will respond with a 404 Not Found.
// Import Express Package
var express = require('express');
var app = express();

// Create root URL
app.get('/'function (reqres) {
   res.send('Hello World');
})

// Start application at Port 3000
const PORT = process.env.PORT || 3000;
var server = app.listen(PORT, () => {
   console.log("Server started at Port: ', PORT);
});

Save the above code in a file named server.js and run it with the following command.
$ node server.js
You will see the following output −
To make our development process a lot easier, we will install a tool from npm, nodemon. This tool restarts our server as soon as we make a change in any of our files, otherwise we need to restart the server manually after each file modification. To install nodemon, use the following command −
npm install -D nodemon
You can now start working on Express using command –
nodemon server.js
Do some changes in server.js file –
app.get('/'function(reqres) {
  res.send('This is Express Server using Nodemon');
});

Now the server will refresh automatically upon saving the server.js file