Send HTML file and JSON Data in Express Js
Send HTML and JSON Data

Serve HTML CSS & JS files in Express JSconst express = require("express");const app = express();const port = 3000;app.get('/', (req, res)=>{res.send("welcome to home page");});app.get("/about", (req, res)=>{res.send("<h1>hello Alians, this is about pages</h1>");});app.get("/contact", (req, res)=>{res.send({id:1,fname:"shreyash",lname:"kolhe"});});app.listen(port, ()=>{console.log("listening",port);})
Serving Static files in Express
To serve static files such as images, css files, and javascript files, use the express.static build-in middleware function in Express.
const path = require("path");const express = require("express");const app = express();// console.log(path.join(__dirname,".."));const staticPath = path.join(__dirname,"..");// builtin middlwareapp.use(express.static(staticPath));app.get("/", (req, res)=>{res.send("hello Alians");});app.listen(8000, ()=>{console.log("listening....");});
Post a Comment