【问题标题】:Node JS How to transform Json response to HTML page in Node JSNode JS 如何在 Node JS 中将 Json 响应转换为 HTML 页面
【发布时间】:2020-09-23 06:37:57
【问题描述】:

我写了一个 nodejs 方法,它发出一个 http.get 请求,其中有一个帖子作为响应。问题是我想将这个 JSon 响应转换为 html 自定义页面。

以下是我写的代码:

router.get('/gettdatadate/', (req, res, next) => {

    let aa=req.query.imei;
    console.log(aa);
    Post.find({'imei.name': req.query.imei, test: { $exists: true } })

      .then((posts) => {
       const json= res.json(posts);
   })
      .catch(err => console.log(err))

  });

我从服务器得到的响应示例:

[
  {
    imei: { name: '865566048694354' },
    test: {
      tactileState: 'ignore',
      pixelState: 'ignore',
      greyState: 'ignore',
      date: '31 mai  a 12:23'
    },
    _id: '5ed393b85e785113d09d1f81',
    __v: 0
  },
  {
    imei: { name: '865566048694354' },
    test: {
      date: '31 mai  a 12:23',
      bafleState: 'ignore',
      microState: 'ignore',
      vibreurState: 'ignore'
    },
    _id: '5ed393bf5e785113d09d1f82',
    __v: 0
  },
  {
    imei: { name: '865566048694354' },
    test: {
      date: '31 mai  a 12:23',
      boutonState: 'ignore',
      kitState: 'ignore',
      usbState: 'ignore',
      batterieState: 'ignore'
    },
    _id: '5ed393c75e785113d09d1f83',
    __v: 0
  },
  {
    imei: { name: '865566048694354' },
    test: {
      date: '31 mai  a 12:24',
      proximiteState: 'ignore',
      lumiereState: 'ignore'
    },
    _id: '5ed393da5e785113d09d1f84',
    __v: 0
  }
]

这是我回复的一般格式,我不知道我应该如何处理才能将其转换为 Html 自定义页面,我需要一些帮助,谢谢

【问题讨论】:

  • 你会回复我的回答吗?

标签: javascript html node.js json express


【解决方案1】:

我找到了一个解决方案,它是使用 ejs 嵌入式 java 脚本模板click here

【讨论】:

    【解决方案2】:

    搜索有关 PugJS 等模板引擎的文档:https://pugjs.org/api/getting-started.html

    【讨论】:

      猜你喜欢
      • 2020-09-22
      • 2013-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-11
      • 1970-01-01
      • 2021-08-28
      相关资源
      最近更新 更多