Tuc*_*uco 16 javascript node.js express
我很新Javascript,node.js并且我正在尝试创建一个REST API,并且网址将是形式的.
/user/{userId}/docs我想获得的价值{userId},这在的情况下/user/5/docs会5.
我可以尝试将其作为请求参数传递(在查询字符串中或在正文中,取决于GET或POST方法),但是当它形成时,url看起来更直观.此外,还有更多这样的网址.
我想知道是否有像express这样的节点模块.
我是一个传统的Java用户和Jersey框架,用于提供这样的东西Java.
谢谢,图科
jos*_*736 68
花一些时间阅读文档.Express使用它:来表示路径中的变量:
app.get('/user/:id/docs', function(req, res) {
var id = req.params.id;
});
Run Code Online (Sandbox Code Playgroud)
在服务器脚本中写入以下内容:
var http = require('http');
var server = http.createServer(function (request, response) {
var url = request.url; //this will be /user/5/docs
url.id = url.split("/")[2]; // this will be 5
response.writeHead(200, {'Content-Type' : 'text/html'});
response.end("Id is = " + url.id);
});
server.listen(8000, '127.0.0.1');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
35357 次 |
| 最近记录: |