0

I am running an express app and in a section I need to pass the page I'm serving some data. I am sending the file with the res.sendFile() function. I would prefer it to be in the form of query parameters, so that the page being sent is able to read them easily.

I am unable to run any templating tool or set cookies since the files are part of a cdn uploaded by users, so the information has to be contained so that it is not easily read by other files also served from my server.

1 Answer 1

1

Query parameters can only be sent by doing a redirect where your server returns a 3xx status (probably 302) which redirects the browser to a different URL with the query parameters set. This is not particularly efficient because it requires an extra request from the server. See res.redirect() for more info.

A more common way to give data to a browser is to set a few Javascript variables in the web page and the client Javascript can then just read those variables directly. You would have to switch from res.sendFile() to something that can modify specific parts of the web page before sending it - probably one of the many template engines available for Express (jade, handlebars, etc...).

You could also send data by returning a cookie with the response, though a cookie is not really the ideal mechanism for variables just for one particular instance of one particular page.

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

4 Comments

Well the issue is that this for a cdn so I have no way of passing information from a template.
@naughtyboy - I don't understand. If your server is doing res.sendFile() to serve the content, then it's coming from your server where you can control what content is being served. If it's coming from a CDN server that you do not control, then it's not coming from your server with res.sendFile(). So, now you've made conflicting statements. Is the file being served from your Express server? Or is it being served by an external CDN that you don't control? Which is it? And, in the future, if you have constraints like this, then please include those in your question.
Its third party content hosted on my server, therefore I cannot/do not have any control over the files being served. I also cannot set cookies since the information should be localized so that other files sent from my server do cannot access the information sent to other files. I am considering the redirection option though, and I apologize for not including the information.
@naughtyboy - You could still inject a <script> tag into the file that sets a few JS variables even though it isn't your content. While streaming the file as the response, you could find the <head> tag and insert a <script> tag into it while in the process of serving the file. You could probably implement a stream filter to do this insertion. The redirect would likely be simpler to implement though.

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.