您好,我遇到了 restify.io 的路由问题
restify 似乎不像 express.js 那样支持可选参数的 "?"。
server.get('/users',function(req,res,next){});
server.get('/users/:id',function(req,res,next{});
// I even tried server.get('/users/',function(req,res,next){});
所以当我运行时一切都按预期工作
http://localhost/users
显示我所有的用户
http://localhost/users/1
显示 id 为 1 的用户
http://localhost/users/ //(note trailing slash)
找不到资源失败,因为这个被解释为空参数而不是路由#1
我不想检查每个参数的空参数并重定向或传递到下一个...
这似乎是一件常见的事情,也应该打击其他人......所以你怎么看待在 url 中没有得到 404 训练斜线
最佳答案
您需要在代码开头附近的某处添加 restify.pre.sanitizePath():
var restify = require('restify');
var server = restify.createServer();
server.pre(restify.pre.sanitizePath()); // Add this line
更多详情,看这个Github Issue .关于 ReST 的原始论文表明斜线具有特殊含义,但是,ReST 不是标准,只是一个指南。因此,斜线的使用/省略取决于 API 设计者的偏好和 API 的语义。一致性是唯一重要的事情。
我模拟并测试了您的设置,并确认可以按照描述解决您的问题:
var restify = require('restify');
var server = restify.createServer();
server.pre(restify.pre.sanitizePath());
var users = [
{ id: 1, name: 'Sean' },
{ id: 2, name: 'Bob' },
{ id: 3, name: 'Ana' }
]
server.get('/users', function (req, res, next) {
console.log(req.query());
res.send(users);
});
server.get('/users/:id', function (req, res, next) {
var user = users.filter(function (user) {
return user.id === req.params.id;
});
res.send(user);
});
server.listen(8080, function() {
console.log('%s listening at %s', server.name, server.url);
});
HTTP 测试:
$ curl localhost:8080/users <- Returns all users
$ curl localhost:8080/users/ <- Returns all users
$ curl localhost:8080/users/1 <- Returns user with id 1
$ curl localhost:8080/users?name=sean <- Logs querystring
$ curl localhost:8080/users/?name=sean <- Logs querystring
关于node.js - 重新调整可选路由参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20662656/
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende
我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere
我正在使用active_admin,我在Rails3应用程序的应用程序中有一个目录管理,其中包含模型和页面的声明。时不时地我也有一个类,当那个类有一个常量时,就像这样:classFooBAR="bar"end然后,我在每个必须在我的Rails应用程序中重新加载一些代码的请求中收到此警告:/Users/pupeno/helloworld/app/admin/billing.rb:12:warning:alreadyinitializedconstantBAR知道发生了什么以及如何避免这些警告吗? 最佳答案 在纯Ruby中:classA
我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"
我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)
两者都可以defsetup(options={})options.reverse_merge:size=>25,:velocity=>10end和defsetup(options={}){:size=>25,:velocity=>10}.merge(options)end在方法的参数中分配默认值。问题是:哪个更好?您更愿意使用哪一个?在性能、代码可读性或其他方面有什么不同吗?编辑:我无意中添加了bang(!)...并不是要询问nobang方法与bang方法之间的区别 最佳答案 我倾向于使用reverse_merge方法:option
我有一个只接受一个参数的方法:defmy_method(number)end如果使用number调用方法,我该如何引发错误??通常,我如何定义方法参数的条件?比如我想在调用的时候报错:my_method(1) 最佳答案 您可以添加guard在函数的开头,如果参数无效则引发异常。例如:defmy_method(number)failArgumentError,"Inputshouldbegreaterthanorequalto2"ifnumbereputse.messageend#=>Inputshouldbegreaterthano
我早就知道Ruby中的“常量”(即大写的变量名)不是真正常量。与其他编程语言一样,对对象的引用是唯一存储在变量/常量中的东西。(侧边栏:Ruby确实具有“卡住”引用对象不被修改的功能,据我所知,许多其他语言都没有提供这种功能。)所以这是我的问题:当您将一个值重新分配给常量时,您会收到如下警告:>>FOO='bar'=>"bar">>FOO='baz'(irb):2:warning:alreadyinitializedconstantFOO=>"baz"有没有办法强制Ruby抛出异常而不是打印警告?很难弄清楚为什么有时会发生重新分配。 最佳答案