Express update a certain html field

Viewed 1026

I have created a simple page that you login with a service and it gets the accounts username using nodejs. I then give the user a file that look like this:

<html>
<body>
<p>Hello <span id="user">user-placeholder</span></p>
</body>
</html>

Index.js: (assume user is logged in as they are)

app.get('/authorized', functiong(req,res){
    console.log(accountInfo.username);
    res.sendFile(file);
});

I just want to update the span that has id user with accountInfo.username. I'm not sure how exactly to update this value.

1 Answers

You should use a template engine like pug by running:

npm install pug --save

Then you index.pug in the views directory

html
  body
    span Hello
    span= user

Then modify your route little bit to look like this:

app.get('/authorized', function (req, res) {
  res.render('index', { user: accountInfo.username });
})

For more details https://expressjs.com/en/guide/using-template-engines.html

Related