Reputation: 9
On my website, when someone visits example.com
, it loads index.html
, which is a very interactive page with lots of JavaScript. What I’d like to do is make it so that if someone enters a URL like example.com/open?(some query)
, the server can process the query, send back some JSON data, but still have the client load index.html
like usual
i want something similar to
app.get(/open,(req,res)=>{
let query=res.query
//proces the query into some data with the database
let data={message:"hello world"}
res.json(data).sendfile(index.html)
})
The client should open index.html
but also have some data that are instructions for some events in my app.
Upvotes: -1
Views: 47
Reputation: 1954
There could be a few workarounds as below.
a) Include the data by modifying response stream by transform method.
Please refer to the answer by Ryan Wheale in the post below.
res.sendfile in Node Express with passing data along
Aside : Please see the accepted answer as well by jfriend00, though he advocates a template engine, he concludes by mentioning the possibility of a simple find and replace just before sending the response.
b) Using data attributes in html.
It requires a template engine to populate data attributes initially from server. Later on the same attributes can be referenced in the front end.
c) Using a cookie.
The same cookie may be referenced later in the front end.
res .cookie('jsondata', JSON.stringify({ a: 1, b: 2, c: 2 })) .sendFile(__dirname + '/' + 'index.html');
Upvotes: 0