What is NodeJS?
As we know that JavaScript is used for make website interactive, so in browser a javascript engine work to convert(compile or interprit) javascript code to machine code, in different browser there are different engines for this work.
Now the question is that can we use javascript in other places not only browser because it is work fast also, so engineers create a runtime environment from chrome's javascript engine which is V8 engine and that standalone runtime environment or JS sngine is called NodeJS, that's the reason why V8 engine is famous.
So using NodeJS we can run javascript not only browser but any of the standalone machine and now javascript is not only used for websites.
Then in NodeJS also added HTTP module means we can now use javascript as server side also now we have concept of full Stack development means front end, backend and database part also.
Then we need framework for creating applications but NodeJS is not a framework, then we got ExpressJS framework.
What is ExpressJS?
Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.
In normal words we can say, Express.js is a small framework that works on top of Node.js web server functionality to simplify its APIs and add helpful new features.
NoseJS have one more thing which is NPM (Node Package Manager) that can serve different modules for making applications more interactive with network, server, database etc and also for different type of dependencies for our applications. It has huge amount of modules that can we use and can also make our modules as well, These are the reasons why NodeJS is very famous now.
Lets get started with Express-
- - First make a directory or folder and change root, like here
$ mkdir myapp
$ cd myapp
- - Now go for package.json initialization for our project
$ npm init
When you run this init command then it will ask for this project details -
After giving all details it will ask for confirm-
and after clicking enter it will create package.json file under directory (myapp).
- - Now we need to install Express in myapp directory, express is coming from third-party server basically it is NPM -
// npm command for install express -
$ npm install express
Now it shows on dependencies we can see on package.json -
- Okay everything is done, now we make a basic "Hello World!" program
We need to use express on our file App.js so first we getting express, and also initialize express and its all functions in next line -
// getting express make it required for our project
const express = require('express');
// and here initalizing express
const app = express();
Now we got express handler and express started.
- Now we need a domain and a port for run our application on browser, for this we run our myapp in localhost and give a port (any like - 9000) or available port for it, you can see here the code-
const express = require('express');
const app = express();
// difining port here
const port = 9000
// Sending response - Hello World
app.get('/',(req, res) => {
res.send('Hello World!')
})
// making a listening port for starting the server
app.listen(port, () => {
console.log(`Your app running on port: ${port}`)
})
You can see output here-
We completed Our Hello World program till here.
- Now we need to access different data which comes with URL like parameter or query string etc, so
- First we getting parameter, here
app.get('/admin',(req, res) => {
res.send(`Welcome Admin!`)
})
Output -
- Second we getting extra parameter, here I am getting parameter after admin which is id, you can use any parameter and can access anything, here
app.get('/admin/:id',(req, res) => {
const id = req.params.id
res.send(`You are Admin!<br /> Your id is ${id}.`)
})
Output -
- Third we also need to get query string,
app.get('/admin',(req, res) => {
const search = req.params.search
res.send(`Your search term is: ${id}`)
})
Output -
Now you know every basic things about ExpressJS.
Tip - Every time when update anything in our file we need to close running and restart again the server (means npm start) and need refresh browser also.
For this we can install nodemon from NPM, it can autometically run again the server we dont need to terminate and run again -
// Installing nodemon
$ npm install nodemon
// Running nodemon
$ npx nodemon
All code here -
const express = require('express');
const app = express();
// difining port here we can use or || and give other port or available port
const port = 9000
// Sending response - Hello World
app.get('/',(req, res) => {
res.send(`<h1>Hello World!</h1>`)
})
// if we want to access query of URL
app.get('/admin', (req, res) => {
const id = req.query.search
res.send(`<h2>Search Query is : ${id} </h2>`)
})
// if we want to access perticular data of URL
app.get('/admin/:id', (req, res) => {
const id = req.params.id
res.send(`<h2>You are Admin!<br />your id is: ${id}.</h2>`)
})
// if we want to access parameter of URL
app.get('/admin', (req, res) => {
res.send(`<h2>Welcome Admin!</h2>`)
})
// making a listening port for starting the server and also prints port
app.listen(port, () => {
console.log(`Your app running on port: ${port}`)
})
Thankyou for reading,
if any suggestion please give me.