如何在 Sails JS 中创建自定义路由 [英] How to create a custom route in Sails JS

查看:45
本文介绍了如何在 Sails JS 中创建自定义路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在 config/routes.js 中添加以下内容,我正在尝试在 Sails 中创建自定义路由,并根据他们的文档:

I'm trying to create a custom route in Sails and according to their documentation, if I add the following in config/routes.js:

'post /api/signin': 'AuthController.index',

请求将由 AuthController 中的 index 操作处理,但这似乎根本不起作用.当我在 Postman 中尝试 /api/login 时,我一无所获.

The request would be dealt with by the index action in the AuthController but that doesn't seems to work at all. When I try the /api/login in Postman, I get nothing back.

请注意,我在 config/blueprints.js 中添加了 restPrefix: '/api'.请注意我使用的是 Sails 0.12.x

Please note that I've added restPrefix: '/api' in my config/blueprints.js. Please note I'm using Sails 0.12.x

我在这里遗漏了什么?

推荐答案

由于您指向一个带有方法索引的控制器,您需要将它添加到您的控制器并从那里发送一个 JSON 响应,(因为您是使用帖子).这是一个简单的例子

Since you are pointing to a controller with method index on it, you need to add it to your controllers and send a JSON response from there, (since you are using post). here is a simple example

'post /api/signin': 'AuthController.index',

api/controllers/AuthController.js

module.exports = {
    index: function(req, res) {
        var id = req.param('id');
        if(!id) {
          return res.json(400, { error: 'invalid company name or token'});
        }
        /* validate login..*/
        res.json(200, {data: "success"};
    }
}

更新

既然你已经有了上面的东西,那很可能是你拥有的蓝图造成的.

Update

Since you already have the above its probably caused by the blueprints you have.

捷径在新的 Sails 应用程序中默认激活,并且可以通过将 sails.config.blueprints.shortcuts 设置为 false 关闭通常在/config/blueprints.js 中.

Shortcut routes are activated by default in new Sails apps, and can be turned off by setting sails.config.blueprints.shortcuts to false typically in /config/blueprints.js.

Sails 为任何控制器/模型对创建快捷路由相同的身份.请注意,对于类似的操作执行相同的操作RESTful/快捷路线.例如,POST/userGETSails 在加载时创建的/user/create 路由api/controllers/UserController.jsapi/models/User.js 将通过运行相同的代码来响应(即使您覆盖了蓝图动作)

Sails creates shortcut routes for any controller/model pair with the same identity. Note that the same action is executed for similar RESTful/shortcut routes. For example, the POST /user and GET /user/create routes that Sails creates when it loads api/controllers/UserController.js and api/models/User.js will respond by running the same code (even if you override the blueprint action)

根据风帆蓝图文档中的说法,可能会关闭关闭快捷方式并删除您添加的前缀.

with that being said from sails blueprint documentation, maybe turning off shortcuts and remove the prefix you've added.

  1. 可能快捷方式正在寻找您的控制器以外的其他地方,从而返回 404.

  1. possibly the shortcuts are looking elsewhere other than your controllers thus returning 404.

前缀被添加到您的蓝图连接路由中,因此您需要 /api/api/signin 来访问它吗?

the prefix is being added to your blueprint connected route, hence you need /api/api/signin to access it?

注意

我无法在我的计算机上复制您的问题,因为它在这里工作正常.但我关闭了所有蓝图设置.

Note

I am unable to replicate your issue on my computer as its working fine over here. but i have all blueprint settings turned off.

module.exports.blueprints = {
  actions: false,
  rest: false,
  shortcuts: false,
  // prefix: '',
  pluralize: false,
  populate: false,
  autoWatch: false,
};

这篇关于如何在 Sails JS 中创建自定义路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆