• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

164
Views
Dynamic endpoint with express

I'm trying to generate a dynamic URL that will change everytime an endpoint is called So I have this simple function in Node.js to create a dynamic url:

var path = crypto.createHash('md5').update(`${Date.now()}`).digest("hex");

function createNewPath(){
  path = crypto.createHash('md5').update(`${Date.now()}`).digest("hex");
}

and this simple code to receive the request:

app.use('/'+path, function(req,res){
  createNewPath();
  res.send("<h1>Welcome!<h1>");
});

The problem is that app.use does not reload the value in "path" so the initial url asigned stays the same, any ideas?

Been breaking my head and got nothing

over 3 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Don't create the route dynamically. Set it up in advance. Check the hash inside it.

Create a route which takes the hash as a param.

Return an error if it doesn't match.

app.get("/:hash", (req, res) => {

    if (req.params.hash === path) {
        return res.send("<h1>Welcome!<h1>");
    }
    return res.sendStatus(404);
});
over 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error