1

I'm using expressJS in a nodeJS server. In one part of my site, I need to response with a HTML page and a JSON. The page is a form, and with the JSON i want to fill the form, so the user can change something and save.

If I use response.json(user), I can get and see the json, but I don't know how to send both (html page of the form and json to fill data).

app.use(express.static('public'));
app.use(express.json());
...
...
app.get('/finish', (request, response) => {
     let user = JSON.stringify(request.user);
     //return html page (form.html in my puclic folder) and 'user' json
}

And if I send, how can I read the JSON with a script tag in html? Have a easy way to do this?

kurose
  • 11
  • 2
  • You could render the form server side and then send the HTML, with the filled in form, that has been rendered. Look for template engines for Node.js. – Emiel Zuurbier Jun 20 '20 at 20:29
  • Your answer is here: https://stackoverflow.com/questions/22551586/write-html-string-in-json You need to code your html and put it inside a json object key, then decode it when receives in client side. – Vahid Alimohamadi Jun 20 '20 at 20:29
  • @cybercoder, then you would also need to parse the HTML, select all the inputs, pass the data from the JSON to the form and append the HTML to the DOM. It seems like a lot of work for something that can be handled server side in a template. – Emiel Zuurbier Jun 21 '20 at 08:50
  • @EmielZuurbier, yep, need something like REACT.JS – Vahid Alimohamadi Jun 21 '20 at 08:53

0 Answers0