Sails.js REST 无操作 [英] Sails.js REST without action

查看:64
本文介绍了Sails.js REST 无操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Sails.js 0.9x 中创建一个 REST API

I'm trying to create a REST API in Sails.js 0.9x

我有一个如下所示的控制器:

I have a controller like the following:

UsersController = {
    list: function(req, res){
            var users [{username: "max"}, {username:'alison'}];
       res.send(users);

     },
    single: function(req, res){
            var user = {username: 'max'};
            res.send(user);
         }

};

如何在不指定操作名称的情况下使用 HTTP 方法和路由访问 URL?

How would I be able to access the URLs using an HTTP Method and the Route without specifying the action name?

例如

HTTP GETmysite.com/users/ 将调用 list 操作

HTTP GET to mysite.com/users/ will invoke the list action

HTTP GETmysite.com/users/1 将调用 single 操作

HTTP GET to mysite.com/users/1 will invoke the single action

目前它只在我调用时有效(这不是我想要的)

Currently it only works when I invoke (which is NOT what I want)

HTTP GETmysite.com/users/list 将调用 list 操作

HTTP GET to mysite.com/users/list will invoke the list action

HTTP GETmysite.com/users/single/1 将调用 single 操作

HTTP GET to mysite.com/users/single/1 will invoke the single action

推荐答案

我认为这可能是一个更好的解决方案

I think this might be a better solution

创建/编辑 config/route.js 并明确定义路由及其相关操作 文档中的链接

module.exports.routes = {
// Standard RESTful routing

// If no id is given, an array of all users will be returned
'get /user/:id?': {
    controller    : 'user',
    action        : 'find'
}
'post /user': {
    controller    : 'user',
    action        : 'create'
}
'put /user/:id': {
    controller    : 'user',
    action        : 'update'
}
'delete /user/:id': {
    controller    : 'user',
    action        : 'destroy'
},

// Override the default index action (find) by declaring an "index" method in your controller
'get /user': {
    controller    : 'user',
    action        : 'index'
}

*/
};

这篇关于Sails.js REST 无操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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