0

Currently, in my app i am hosting a nodejs web server with express. All I want to do is generate a html file with unique name , create a route with this name and redirect server to this route. I have already generated file with unique name but I can't find my way with routes. Is it possible to work this way?

1

1 Answer 1

1

The most intuitive solution seems for me the one that would use the show action in RESTful routing and which consists in using, for your case, the file names as parameters of the request.

Basically, you'd have something like this:

app.get('/files/:uniqueHtmlFileName', function(req , res){ 
    res.sendfile(req.params.uniqueHtmlFileName+".html");
  });

More on:

N.B: The code I showed will probably not work right of the bat as you'd probably need to take care of path issues, refer to the sendFile() doc for a more comprehensive code example.

Sign up to request clarification or add additional context in comments.

2 Comments

This worked. But i am stucked somewhere else. Do you know how can I navigate to this url automatically? Specifically, in a js file I do some calculations inside a function and I want to redirect browser automatically (user doesn't need to do any action) after this function completes.
First thing that comes to mind is a redirect with res.redirect("destination-url-here"), feel free to share further details if this does not cut it. Check-out this link: expressjs.com/fr/api.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.