【问题标题】:How can I serve dynamic content in Node.js using StrongLoop?如何使用 StrongLoop 在 Node.js 中提供动态内容?
【发布时间】:2015-02-18 10:51:38
【问题描述】:

我对 Node.js 还很陌生,但 StrongLoop Docs 并没有真正给出任何具体的例子。我正在为我的模板使用 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 上工作。

【问题讨论】:

  • 你找到答案了吗?我正在尝试将骨架环回应用程序转换为全栈应用程序/api 服务器(可以从服务器外部和内部访问数据)。

标签: javascript node.js strongloop


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

我在这里维护了这个用例的完整示例:https://github.com/strongloop/loopback-faq-middleware#how-do-you-serve-static-content-via-static-middleware

如果您需要更多帮助,请告诉我。 ;)

【讨论】:

  • OP 要求的是如何处理环回中的动态页面。你说的是静态页面..
猜你喜欢
  • 2011-07-12
  • 1970-01-01
  • 2016-05-02
  • 1970-01-01
  • 2017-10-25
  • 1970-01-01
  • 1970-01-01
  • 2017-09-23
  • 2018-12-25
相关资源
最近更新 更多