gpt4 book ai didi

Sails.js 将端点方法与 http 方法(GET、POST)匹配

转载 作者:行者123 更新时间:2023-12-01 11:41:17 27 4
gpt4 key购买 nike

我正在研究 Sails.js 上的文档,并根据他们的“入门”指南创建了一个基本的开发实例:

$ sudo npm -g install sails
$ sails new testProject
$ sails generate users
$ sails lift

这四个简单的命令将按照设计安装平台、创建项目并创建模型 users

从我的浏览器,使用默认安装端口,我现在可以访问 http://localhost:1337/users 并按预期接收一个空的 JSON 数组 [] .

现在,我是否希望创建一个新用户REST最佳实践规定我应该使用 POST 方法。 POST 确实有效,但是开箱即用,Sails 允许您执行GET http://localhost:1337/users/create 生成一个新的用户对象。

通读了他们的文档,我一直无法确定一种方法来限制允许哪些 HTTP 方法执行各种任务。这在文档中吗?或者有人可以解释在 Sails 堆栈中的什么地方(应该|可以)管理它?

最佳答案

Sails.js 为您提供那些 shortcuts让您仅出于开发目的使用 GET 方法进行操作。如果需要,您可以使用文档中记录的配置禁用生产中的那些:

By default, Sails will create a blueprint action route for each action in a controller, so that a GET request to /:controllerIdentity/:nameOfAction will trigger the action. If the example controller in the previous section was saved as api/controllers/SayController.js, then the /say/hi and /say/bye routes would be made available by default whenever the app was lifted. If the controller was saved under the subfolder /we, then the routes would be /we/say/hi and /we/say/bye. See the blueprints documentation for more information about Sails’ automatic route binding.

Besides the default routing, Sails allows you to manually bind routes to controller actions using the config/routes.js file. Some examples of when you might want to use explicit routes are:

  • When you want to use separate actions to handle the same route path, based on the HTTP method (aka verb). The aforementioned action blueprint routes bind all request methods for a path to a given action, including GET, POST, PUT, DELETE, etc.

关于Sails.js 将端点方法与 http 方法(GET、POST)匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20501167/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com