【问题标题】:is it possible to use ES6 template literals instead of view engines for express.js?是否可以使用 ES6 模板文字而不是 express.js 的视图引擎?
【发布时间】:2020-07-06 18:12:08
【问题描述】:

我开始使用 Node 和 Express 进行后端 Web 开发,并且我看到了一个使用视图引擎将网站呈现为服务器响应的教程。我想到了一个问题,我是否可以使用模板文字来做同样的功能吗?

在前端我曾经使用车把,但我放弃了它只是开始使用这些文字,因为它们具有完全相同的功能..云后端也一样吗?以及如何?

我可以创建一个包含模板的 js 文件并将其直接设置到视图引擎吗?

【问题讨论】:

    标签: javascript node.js express ecmascript-6 backend


    【解决方案1】:

    您可以简单地使用res.send()

    body 参数可以是 Buffer 对象、String、对象或 大批。例如:

    res.send(Buffer.from('whoop'))
    res.send({ some: 'json' })
    res.send('<p>some html</p>')
    res.status(404).send('Sorry, we cannot find that!')
    res.status(500).send({ error: 'something blew up' })
    

    即:

    const html = `
    <html>
      <head>
        <title>overrides</title>
      </head>
      </body>
        <div>test</div>
      </body>
    </html>
    `
    app.get('/test', (req,res) => {
      res.send(html)
    });
    

    这将输出:

    您可以使用module.exports 导出代码块。

    【讨论】:

      猜你喜欢
      • 2017-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-10
      • 2015-08-02
      • 1970-01-01
      • 2017-03-16
      • 2020-07-22
      相关资源
      最近更新 更多