Node JS
1. what is Node JS ?
2.
Node Js Simple demo
var http = require('http');
http.createServer(function(req,res){
res.write("Hello World! Welcome to Node Js Learning");
res.end();
}).listen(8080);
i. go to cmd and go to path for above program by cd command
ii. type node space fileName.js
iii. now open browser and type http://localhost:8080/ enter.
iv. it will display " Hello World! Welcome to Node Js Learning "
3. Node JS Architecture
4. NPM (Node Package Manager)
5. Package.JSON File
6. Express JS
7. Building REST Api using Node JS
i. go to terminal and type "npm init" enter
ii. "npm install --save express" enter
iii. after above command
'node_modules'
'package-lock.json' and
'package.json' will automatically created
iv. now create an index.js file and code like below for rest api
// this is index.js file.
const express = require('express')
const app = express()
const port = 3000;
// parse JSON using express
app.use(express.json());
app.use(express.urlencoded({extended: false}));
let movies = [
{
id: "1",
title: "Inception",
director: "Christina",
Release_date: "2010-08-01",
},
{
id: "2",
title: "Transformer",
director: "Donald",
Release_date: "2020-04-12",
},
]
// get the movie list in the form of JSON
app.get('/movie',(req,res) => {
res.json(movies)
})
// add movie list to the list
app.post('/movie', (req,res) =>{
const movie = req.body
console.log(movie)
movies.push(movie)
res.send('Movie is added to the list.');
});
// Search for a movie in the list
app.get('/movie/:id', (req,res) =>{
const id = req.params.id
for(let movie of movies){
if(movie.id===id){
res.json(movie)
return
}
}
res.status(404).send("Movie not found.")
})
// remove movie from the list
app.delete('/movie/:id', (req,res) =>{
const id = req.params.id;
movies = movies.filter((movie) => {
if(movie.id !== id){
return true;
}
return false;
});
res.send('Movie is deleted.')
});
// set the server to listen at port
app.listen(port, () => console.log(`Server listing at port ${port}`));
now run it on Localhost port 3000.
and manage it from postman api management.
8. Node JS Authentication with JWT
(Json Web Token)
Node JS Authentication with JWT (it's not work perfectly fix it)
// this is index.js file.
const express = require('express')
const jwt = require('jsonwebtoken')
const app = express();
app.get('/api', (req, res) => {
res.json({
message: "Hey there! Welcome to this API Service",
});
});
app.post("/api/posts",verifyToken ,(req,res) => {
jwt.verify(req.token, "secretary",(err, authData) => {
if(err) {
res.sendStatus(404); // forbidden
}else {
res.json({
message: "Posts Created...",
authData,
});
}
});
});
app.post("/api/login", (req,res) => {
const user = {
id: 1,
username: "John",
email: "John@gmail.com",
};
jwt.sign({ user: user}, "secretary", (err, token) =>{
res.json({
token,
});
});
});
function verifyToken(req,res) {
const bearHeader = req.header['authorization']
if(typeof bearHeader !== undefined){
const bearerToken = bearHeader.split(' ')[1]
req.token = bearerToken
next()
}else{
res.sendStatus(403)
}
}
app.listen(3000, (req, res) =>{
console.log("Service started on port 3000")
});
i. go to terminal and type "npm init" enter
ii. "npm install --save express" enter
iii. after above command
'node_modules'
'package-lock.json' and
'package.json' will automatically created
iv. now create an index.js file and code like below for rest api
now run it on Localhost port 3000.
and manage it from postman api management.
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)