I am creating a web app where a user can type in /poll/:id in order to retrieve a poll with the given id from my database. Presumably, in order to do this, my Node JS express server would first need to send over the HTML I want the poll to be displayed on, the JavaScript that handles the frontend and then JSON data representing the poll.
I am running into a roadblock because I have no idea how to handle such a request. As a jumping off point, I tried to make my server simply send over the html page in question upon receiving the request (and hence not using express's static server). The code is as follows:
app.get('/poll/:id', function(req, res, next){
res.sendFile('poll.html');
});
However, the page on the user's end does not handle any of the CSS, hence the page is not styled at all upon being sent to the user, nor is the frontend JS supported. What exactly do I have to do in order to ensure all of that is functional in the frontend? Is there an easier approach to this that I am not realizing?
If you want to generate the HTML on the server-side, use a templating engine, ejs for example. There u would be able to retrieve the data from the database and easily render the page according to that data. On the other hand, if you want to retrieve the data using javascript then just serve the HTML files statically, and then create another route which would only respond with the JSON object retrieved from the database, (which the frontend would request), and then the frontend-javascript would render the application