What is Express js
Express is a most popular node.js framework that provides a robust set of features for web and mobile applications.
First we need to install node.js.to do this we have to download node.js from official node.js website.then install on our computer.
What is Node.js
Node.js is an open source server environment.It is runs various platforms.Node.js uses javascript on the server.
Node.js can generate dynamic web pages,CRUD operations on the server etc.
Now let's try to display 'Hello world!' on the web browser using Node.js
To do this create server.js file and add the following code.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World!');
}).listen(8080);
Navigate to the folder that contains the file server.js via terminal.
Then type
node server.js
Now open a web brower and type http://localhost:8080/
Now computer works as a server.You can see 'Hello world' message on the web browser.
Install express framework using NPM
Now we have an idea about how Node.js works.We try to do same thing using Express Framework.First create project directory called express.type following command.You can find more information about Express by clicking this link.
npm init
This command prompts you for a number of things, such as the name and version of your application. For now, you can simply hit RETURN to accept the defaults for most of them.You can see a file called package.json generated automatically.(package.json is a plain JSON(Java Script Object Notation) text file which contains all metadata information about Node JS Project or application)
Then we have to install express to our project.to do this use the following code.
npm install express --save
Now Let's try Hello world example using Express Framework.
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
Now open the web browser as in the previous step and go to http://localhost:3000
You can see Hello world message on the web browser.This is very basic of Node.js.We will talk more about this in future articles.Keep learning..
Post a Comment