# Express Js
- instal express
- install nodemon
- setup an app
## 1. middleware
-create a basic middleware and show the iportance of the next param
```
function printUrl(req, res, next){
console.log(req.OriginalURL);
next();
}
```
## 2. express.json
```
app.use(express.json())
app.post('/form', (req, res) =>{
const data = req.body
console.log(data);
return res.send(`Username: ${data?.username} | Password: ${data?.password}`)
})
```
## 3. body parser
Used Before Express 4.16+ to parse Json Format
now the express.json handles json parsing
```
const bodyParser = require('body-parser')
app.use(bodyParser.json())
```
## 4. express url encoded
```
app.use(express.urlencoded({ extended: true }));
set the content-type header and body to match urlencoded http request
```
## 5. morgan
visualise the request status code
```
npm i morgan
```
## 6. helmet
used for security layers.
```
npm i helmet
```
## 7. cookies parser
```
npm i cookie-parser
const express = require('express')
const cookieParser = require('cookie-parser')
const app = express()
app.use(cookieParser())
app.get('/',(req, res) =>{
res.json(req.cookies);
})
app.get('/setcookie/:name', (req, res)=>{
if(req.params.name){
res.cookie(req.params.name, 'hello', {
maxAge: 10*1000
})
res.send(`cookie set to ${req.params.name}`)
}
})
app.listen(5000)
```
## 8. cors