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 Answer
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.
2 Comments
Manos Kaparos
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.
Akheloes
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