简体   繁体   English

SailsJS-创建没有蓝图的版本化API

[英]SailsJS - Create versioned API without blueprint

I'm trying to figure out how to create a versionned API for my application. 我试图弄清楚如何为我的应用程序创建版本化的API。 According to this issue https://github.com/balderdashy/sails/issues/322 , I have to use blueprint, but all blueprint is deactivated in my project (a wish from my boss). 根据这个问题https://github.com/balderdashy/sails/issues/322 ,我必须使用蓝图,但是所有蓝图都在我的项目中被停用(老板的一个愿望)。

I want to be able to have any URL like http://myapi.com/v1/my-custom-route , and so on for any version. 我希望能够拥有任何版本的URL,例如http://myapi.com/v1/my-custom-route ,等等。

So far, the best way I've found is to duplicate all controller to something like v322AuthController.js and to map all routes like 到目前为止,我发现的最好方法是将所有控制器复制到v322AuthController.js并映射所有路由,例如

'POST /v3.2.2/se-connecter'              : 'v322AuthController.perform_signin'

But I think that's an ugly trick. 但是我认为这是一个丑陋的把戏。 I'm currently using Nginx and all my code is versionned with git 我目前正在使用Nginx,所有代码都使用git版本控制

Thank you if you have any idea 谢谢你有什么想法

Kai23 Kai23

you have to disable all blueprints? 您必须禁用所有蓝图? Than you have to write all your routes an our own. 比起您必须自己编写所有路线而言。

If you don't want to duplicate your controllers you can try this: 如果您不想复制控制器,则可以尝试以下操作:

config/routes.js

module.exports.routes = {

 'post /:apiversion/se-connecter' : {
    controller: 'AuthController',
    action: 'perform_signin',
    skipAssets: true
 }
....
}

So Sails passes all */se-connecter to the method "perform_signin" at your "AuthController". 因此,Sails将所有* / se连接器都传递给“ AuthController”中的方法“ perform_signin”。 In your controller you have your api-version: 在控制器中,您可以使用api版本:

AuthController.js

module.exports = {
   perform_signin: function (req, res) {

      var apiversion = req.param('apiversion');

      if (apiversion === "0.2.0") {
         ....
      }
   }

}

My approach: 我的方法:

Create subfolder for your controllers as: 为您的控制器创建子文件夹为:

/controllers/v1/UserController.js
/controllers/v2/UserController.js

In your routes.js file, add as follow: 在您的routes.js文件中,添加如下内容:

'POST /api/v2/user': 'v2/UserController.create',
'POST /api/v1/user': 'v2/UserController.create',

And in your policies.js, you can add middlewares like this: 在您的policy.js中,您可以添加如下中间件:

  'v2/UserController': {
    'create': ['isAuthenticated','isAuthorized']
   }

My current sails' version is: 0.12.3 我当前的航行版本是:0.12.3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM