【问题标题】:Specifying routes in sailsjs在sailsjs中指定路线
【发布时间】:2015-12-08 08:35:41
【问题描述】:

我对sails.js 中的路由有疑问。 所以,我正在关注有关制作登录页面的教程。它包括 AuthController.js

module.exports = {
    login: function(req , res){
        res.view('login'); //view login page
    },

    authenticate: function(req, res) {
        //some auth function
    }
};

login.ejs

<div id="login">
    <form align="center" action="/login" method="post">
    <ul>
        <li>
        <input type="text" name="username" placeholder="Username"></li>
        <li>
        <input type="password" name="password" placeholder="Password" ></li>
        <li>
        <input type="submit" value="Log In"></li>
    </ul>
    </form>
</div>

最后这让我对routes.js 感到困惑。为什么会这样?

  'get /login': {
    controller: 'AuthController',
    action: 'login'
  },

  'post /login' : {
    controller: 'AuthController',
    action: 'authenticate'
  },

但这没有(我删除了get)?

  '/login': {
    controller: 'AuthController',
    action: 'login'
  },

  'post /login' : {
    controller: 'AuthController',
    action: 'authenticate'
  },

当我使用后面的路由时,当我输入用户名密码时,似乎永远不会调用 authentication 操作,它只是将我再次重定向到登录页面(它正在调用 login 操作)。

【问题讨论】:

    标签: node.js sails.js


    【解决方案1】:

    来自sails documentation

    If no verb is specified, the target will be applied to any request that matches 
    the path, regardless of the HTTP method used (GET, POST, PUT etc.).
    
    
    URLs are matched against addresses in the list from the top down.
    

    顺序也是从上到下。因此,当您尝试在/login 中发布时,它会再次转到/login 而不是POST /login

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      正如其他人所说,这是因为路由是按顺序比较的,先匹配哪个触发。

      有趣的是,这意味着如果您交换订单,它会按照您的描述工作:

      'post /login' : {
        controller: 'AuthController',
        action: 'authenticate'
      },
      '/login': {
        controller: 'AuthController',
        action: 'login'
      },
      

      【讨论】:

        【解决方案3】:

        在sails Js 中,route.js 由一个地址(在左侧,例如'get /login')和一个目标(在右侧,例如'AuthController.login')组成。地址是一个 URL 路径和(可选)一个特定的 HTTP 方法。当 Sails 收到传入请求时,它会检查所有自定义路由的地址是否匹配。如果找到匹配的路由,则将请求传递到其目标。

        现在,当您删除 get 选项并提升您的应用程序并导航到 /login 时,首先会呈现登录页面,但是当发布表单时,由于您省略了 get ,因此无法区分 b/w 请求请求,所以它再次调用 /login 并且永远不会到达 post 路由。

        参考:http://sailsjs.org/documentation/concepts/routes

        【讨论】:

          猜你喜欢
          • 2019-04-30
          • 1970-01-01
          • 2015-07-08
          • 1970-01-01
          • 2016-08-19
          • 2015-05-05
          • 2023-03-23
          • 1970-01-01
          • 2014-09-16
          相关资源
          最近更新 更多