ExpressJS
What is ExpressJS?
Express.js is a Node.js web application framework that provides features for building web and mobile applications. It is one of the most popular frameworks for Node.js due to its simplicity, ease of use, and extensive ecosystem of middleware.
Why ExpressJS?
Setting up an ExpressJS Project
npm init -y
This creates our project.
npm install express
This installs Express as our dependency for our project.
const express = require('express')
require('dotenv').config()
const app = express();
const port = process.env.PORT || 8000;
app.use("/", (req, res)=>{
res.json({
name:"Aastha",
age:"19",
profession:"Student",
address:{
temporary_address:"Kathmandu",
permanent_address:"Jhapa"
},
message:"This server is developed by Aastha"});
})
app.listen(port, () => {
console.log(`Server started on ${port}`);
})
npm i dotenv
Once installed, we create a .env file which stores our environment variable. In this case,
PORT=5000
node server.js
Thanks to our mentor, Bikash Karki , for his insightful lecture.