Mern
/**
* This is hoe you create a server
*/
const dotenv = require("dotenv");
const express = require("express");
const { default: mongoose } = require("mongoose");
const app = express();
const PORT = process.env.PORT
dotenv.config({ path: './config.env'});
// Database connection
const DB = process.env.DATABASE
/**
* Connect method will connect server to the database
*/
mongoose
.connect(DB)
.then(() => {
console.log("Connection successful");
})
.catch((err) => {
console.log(err);
});
// Middleware ---
const middleWare = (req, res, next) => {
console.log("Helllo middleware");
next();
};
/**
* Route to get the data
*/
app.get("/", middleWare, (req, res) => {
// It will be displayed in browser
// res.send is for sending the data
res.send("Hello from the server");
});
app.get("/about", middleWare, (req, res) => {
res.send("Hello from About");
});
/**
* listen method will start server
*/
app.listen(PORT, () => {
console.log(`server isrunning on ${PORT}`);
});
Raviraj Solanki