Skip to content

External libraries

Course: PayTM project - chapter 5

Since our frontend and backend will be hosted on separate routes, add the cors middleware to backend/index.js

Hint

Look at https://www.npmjs.com/package/cors

Solution
backend/index.js
const express = require('express');
const cors = require("cors");
app.use(cors());
const app = express();
module.exports = router;

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

Hint

https://medium.com/@mmajdanski/express-body-parser-and-why-may-not-need-it-335803cd048c

Solution
backend/index.js
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);

We will be adding authentication soon to our application, so install jsonwebtoken library. It’ll be useful in the next slide

npm install jsonwebtoken

Export a JWT_SECRET from a new file backend/config.js

Solution
backend/config.js
module.exports = {
JWT_SECRET: "your-jwt-secret"
}

Make the express app listen on PORT 3000 of your machine

Solution
backend/index.js
... Existing code
app.listen(3000);