External libraries
Course: PayTM project - chapter 5
1. Add cors
Section titled “1. Add cors”Since our frontend and backend will be hosted on separate routes, add the cors middleware to backend/index.js
Hint
Solution
const express = require('express');const cors = require("cors");
app.use(cors());
const app = express();
module.exports = router;2. Add body-parser
Section titled “2. Add body-parser”Since we have to support the JSON body in post requests, add the express body parser middleware to backend/index.js
You can use the body-parser npm library, or use express.json
Solution
const express = require('express');const cors = require("cors");const rootRouter = require("./routes/index");
const app = express();
app.use(cors());app.use(express.json());
app.use("/api/v1", rootRouter);3. Add jsonwebtoken
Section titled “3. Add jsonwebtoken”We will be adding authentication soon to our application, so install jsonwebtoken library. It’ll be useful in the next slide
npm install jsonwebtoken4. Export JWT_SECRET
Section titled “4. Export JWT_SECRET”Export a JWT_SECRET from a new file backend/config.js
Solution
module.exports = { JWT_SECRET: "your-jwt-secret"}5. Listen on port 3000
Section titled “5. Listen on port 3000”Make the express app listen on PORT 3000 of your machine
Solution
... Existing code
app.listen(3000);