Manos Kaparos
Manos Kaparos

Reputation: 139

Redirect to generated url with express (nodejs)

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?

Upvotes: 0

Views: 830

Answers (1)

Akheloes
Akheloes

Reputation: 1372

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.

Upvotes: 1

Related Questions