我正在尝试将Connect用于Node.js,但在让路由器中间件完全按照我的意愿进行操作时遇到了问题。
具体来说,我希望能够使用URL的搜索词部分填充req.params对象,例如
http://www.google.com/search?q=Search
会返回req.params = { "q" : "Search" }
我更喜欢一个设置,我可以做一些事情:
router.get('/user?:userid&:fullname&:emailaddress', function(req, res) {...
这样我就可以访问每个搜索字词的访问权限,然后做出相应的响应。
有没有办法在Connect中执行此操作?
答案 0 :(得分:4)
这不是使用Connect解决问题的真正答案。但我认为你应该使用Express(由同一作者TJ)使用Connect,但功能更强大。
Sinatra启发了网络开发 node.js的框架 - 疯狂 快速,灵活,性感
使用Express实现时,这非常简单,如app.js所示:
var express = require('express'),
app = express.createServer();
app.get('/search', function (req, res) {
res.send(req.query);
});
app.listen(3111);
alfred@alfred-laptop:~/node/so/6513101$ curl -v "http://localhost:3111/search?q=a&y=b"
* About to connect() to localhost port 3111 (#0)
* Trying ::1... Connection refused
* Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 3111 (#0)
> GET /search?q=a&y=b HTTP/1.1
> User-Agent: curl/7.21.0 (i686-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18
> Host: localhost:3111
> Accept: */*
>
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Content-Type: application/json; charset=utf-8
< Content-Length: 17
< Connection: keep-alive
<
* Connection #0 to host localhost left intact
* Closing connection #0
{"q":"a","y":"b"}