0

我正在尝试 node 和一些用于 node atm 的框架,特别是机车。但是,我似乎被困在使用机车的路线上。有几个问题我找不到答案,所以这里是:

  1. 为什么机车开箱即用安装使用 index.html.ejs 作为文件名?为什么不只是 index.ejs?有什么好处?

  2. 我正在尝试将路由添加到视图:我在视图文件夹中添加的 searchName.html.ejs。为了实现这一点,我制作了一个这样的工具控制器:

    var locomotive = require('locomotive').Controller,
    toolController = new Controller();
    
    toolController.searchName = function() {
        this.render();
    }
    
    module.exports = toolController;
    

    我还在 routes.js 中添加了一条路线,如下所示:

    this.match('searchName', 'tool#searchName');
    

    但是,这不起作用(但这是文档所说的应该起作用的)。结果是 404 错误。那么我如何使这条路线工作?

  3. 假设我想创建一条到例如 anExample.html 的路线?我该怎么做?我注意到在机车的开箱即用应用程序中,您无法输入 localhost:3000/index.html 。甚至 localhost:3000/index 这对我来说似乎非常不切实际,因为有很多用户会添加他们想要访问的特定页面。那么我怎样才能使它工作呢?

PS:我在stackoverflow上解决了所有关于此的问题并搜索了网络,但我仍然无法弄清楚这一点。enter code here

4

1 回答 1

1
  1. 好处是这种命名方案允许您为单个路由指定几种不同的格式。因此,您可以拥有search_name.html.ejsand search_name.xml.ejs,然后根据客户的期望使用任一视图进行响应。

  2. 您发布的示例代码存在几个问题。您应该看到比404,所以我不确定那里发生了什么,但这里是在我的环境中工作的代码修复。

    在控制器中:

    //tool_controller.js
    var locomotive = require('locomotive');
    var toolController = new locomotive.Controller();
    
    toolController.searchName = function() {
      this.render();
    };
    
    module.exports = toolController;
    

    在 routes.js 中:

    //routes.js
    module.exports = function routes()
    {
      this.match('searchName', 'tool#searchName');
    }
    

    然后,您需要将视图更改为:views/tool/search_name.html.ejs. 从文档中并不清楚,但机车会自动小写并强调骆驼大小写的动作,如 searchName。

    现在启动应用程序并浏览到http://localhost:3000/searchName

  3. 如果您只想提供静态 html 文件,最简单的方法是将其放入public文件夹中。此文件夹专门用于提供静态内容,如客户端 js、css 等。它也适用于提供静态 HTML。

于 2014-04-22T05:07:10.893 回答