Last active
October 25, 2022 04:36
-
-
Save Deeks900/132600f4e980cffc1826335ab54e234d to your computer and use it in GitHub Desktop.
Express Server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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