Lesson 4 - Creating first NodeJS API (End Point) | Learning MERN Stack

 Whenever we are trying to fetch the data from our backend, the API is the point that helps us in doing that.

Let's create our first API : 

Go to the index.js file and paste the below code : 

index.js : 

----------

const express = require("express");
const app = express();

app.get("/", (reqres=> {
  res.send("API is running");
});

app.listen(5000console.log("server started"));

Now run the server: npm start (which actually means:  nodemon backend/index.js)

Setting up .env : 

> create a file with name .env

> npm i dotenv

.env : 

----------

PORT = 6000


index.js :

------------

const express = require("express");
const dotenv = require("dotenv"); //added
const app = express();
dotenv.config(); //added

app.get("/", (reqres=> {
  res.send("API is running");
});

const PORT = process.env.PORT || 5000; //added
app.listen(PORTconsole.log(`server started at ${PORT}`));


Setting up postman in VS CODE : 

Just install thunder client in VS CODE

0 Comments