Sunday, 29 March 2020

Express Js

    ExpressJS is a web application framework that provides you with a simple API to create websites, web apps and back ends. When use ExpressJS developer does not need not bother about low-level protocols, processes, etc.
Express provides a very simple interface to build applications. simply, gives the tools that are needed to build the application. It is manageable as there are various modules available on npm and they can be directly plugged into Express JS. Express was developed by TJ Holowaychuk but now it is maintained by the Node.js  Foundation along with several source contributors.


Why Express So important?




Express JS does not predefine the best way for building applications and it is completely upon the developer. that option does not available in other competitive frameworks. so that It is very flexible and pluggable.

Let's build a simple app



To begin with, Express, the Node and the npm (node package manager) should be installed in your computer. To check whether these are in your pc or not you can use following commands.



node --version

npm --version

Whenever we create a project using npm, we need to provide a package.json file, that includes all the details about the project. using npm it is very easy to set up this file. now let's set up the project. there are three steps.
Step 1 : - 
open the terminal/cmd, create a new folder named hello-world and change the directory into it.

step 2 : -
then to create the package.json file using npm, run the bellow comand.
npm init
then it will requirest some information just keep press enter and finally give your name as author name.

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 verify that Express has installed correctly, execute the below command.
ls node_modules #(dir node_modules for windows)

now we sucessfully set up the development, the to develop the first app using Express. Create a new file called index.js and type the following in it.




var express = require('express');
var app = express();

app.get('/', function(req, res){
   res.send("Hello world!");
});


app.listen(3000);


Save the file, go to your terminal and type the following.
nodemon index.js

This will start the server. To test this app, open your browser and go to http://localhost:3000 and a message will be displayed as in the following screenshot.




















No comments:
Write comments

© 2014 Geeks Of Geeks. Designed by Bloggertheme9
Powered by Blogger.