Skip to content

Instantly share code, notes, and snippets.

@Deeks900
Last active October 25, 2022 04:36
Show Gist options
  • Save Deeks900/132600f4e980cffc1826335ab54e234d to your computer and use it in GitHub Desktop.
Save Deeks900/132600f4e980cffc1826335ab54e234d to your computer and use it in GitHub Desktop.
Express Server
const express = require('express');
const app = express();
app.listen(3000);
const express = require('express');
const app = express();
app.listen(3000);
//sending response using express
app.get('/about', (req, res)=>{
res.send("Hello guys!!");
})
//sending an index file as response using relative path
app.get('/about', (req, res)=>{
res.sendFile('./about.html', {root:__dirname});
})
//Or we can pass absolute path
app.get('/about', (req, res)=>{
res.sendFile('D:/Projects/server/about.html');
})
//redirect
app.get('/about-us', (req, res)=>{
res.redirect('/about');
})
//This is a middleware function.This function will run for sure in a file if no case hits before this
app.use((req, res)=>{
res.status(404).sendFile('./404.html', {root:__dirname})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment