如何使用StrongLoop在Node.js中提供动态内容

How can I serve dynamic content in Node.js using StrongLoop?

本文关键字:动态 js 何使用 StrongLoop Node      更新时间:2023-11-11

我对Node.js还很陌生,但StrongLoop文档并没有给出任何具体的例子。我使用Handlebars作为模板,只是尝试提供一个标准的index.js预编译模板。我知道我应该编辑我的routes.js文件,但我不知道怎么做。

这就是我的routes.js文件的样子:

module.exports = function(app) {
    // I need this to serve my index.js file.
    app.get('/', function(req, res) {
        // Doesn't work, so I commented it out:
        // require('views/index.js');
        // Doesn't work, so I commented it out:
        // res.requires('views/index.js');
        // Doesn't work, so I commented it out:
        // res.send('views/index.js');
        // What do I put here then?
    });
    // This is the generic example. Just sends text.
    app.get('/ping', function(req, res) {
        res.send('pong');
    });
};

只是为了澄清一下,当我说它不起作用TM时,我的意思是键入localhost显示404全包路线。是的,那是localhost,而不是localhost:3000。它在80号端口工作。

  1. 将静态中间件添加到middleware.json中的files属性中
  2. 删除server/boot/root.js
  3. 创建客户端/index.html
  4. 通过slc运行启动服务器并浏览到localhost:3000

我在这里维护了这个用例的完整示例:https://github.com/strongloop/loopback-faq-middleware#how-您是否通过静态中间件提供静态内容

如果你还需要帮助,请告诉我。)