Created
May 24, 2022 16:06
-
-
Save BlazerYoo/4907bfdb214685ad7ccd246b4e66fbc5 to your computer and use it in GitHub Desktop.
ExpressJs get full request url [Credits: https://www.geeksforgeeks.org/how-to-get-the-full-url-in-expressjs/]
This file contains 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
// Credits: https://www.geeksforgeeks.org/how-to-get-the-full-url-in-expressjs/ | |
const express = require('express'); | |
const app = express(); | |
const PORT = 3000; | |
app.get('*', function (req, res) { | |
const protocol = req.protocol; | |
const host = req.hostname; | |
const url = req.originalUrl; | |
const port = process.env.PORT || PORT; | |
const fullUrl = `${protocol}://${host}:${port}${url}`; | |
const responseString = `Full URL: ${fullUrl}`; | |
res.send(responseString); | |
}) | |
app.listen(PORT, (error) => { | |
if(!error) | |
console.log("Server running, app listening on port", PORT) | |
else | |
console.log("Error:", error); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment