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?
Asked
Active
Viewed 796 times
0
-
Hey, I guess this might help you: https://stackoverflow.com/questions/25623041/how-to-configure-dynamic-routes-with-express-js – Akheloes Jun 08 '18 at 20:17
1 Answers
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.

Akheloes
- 1,352
- 3
- 11
- 28
-
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. – Manos Kaparos Jun 25 '18 at 14:50
-
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: http://expressjs.com/fr/api.html – Akheloes Jun 25 '18 at 15:30