将数据从路由器发送到前端js的方法

问题描述 投票:1回答:1

在node.js服务器上,我具有用于获取请求的路由器:

router.js:

   router.get("/car/index", auth, async (req, res) => {
      console.log(req.user); // prints a name of a logged user
      res.sendFile(path.join(__dirname, "../../public/car.html"));
    });

在获取请求时,我在req.user参数中保存了一个登录用户的名称。我需要将此名称发送到我的前端javascript。这意味着,要与car.html网页连接的前端javascript文件carController.js:

car.html:

<!DOCTYPE html>
<html lang="en">
  <body>
    ..........
    <script
      type="module"
      defer
      src="/js/controllers/carController.js"
    ></script>
 </body>
</html>

任何制造方式?

javascript node.js send
1个回答
0
投票

您是否考虑过使用ejs? (https://ejs.co/

您可以呈现html页面并嵌入javascript数据。在网页中,您有一些示例。

© www.soinside.com 2019 - 2024. All rights reserved.