restify

Restify 是一个 Node.JS 模块,可以让你创建正确的 REST web services。它借鉴了很多 express 的设计,因为它是 node.js web 应用事实上的标准 API。

示例代码:

服务器端:

var restify = require(‘restify’);

var server = restify.createServer({
name: ‘myapp’,
version: ‘1.0.0’
});
server.use(restify.acceptParser(server.acceptable));
server.use(restify.queryParser());
server.use(restify.bodyParser());

server.get(’/echo/:name’, function (req, res, next) {
res.send(req.params);
return next();
});

server.listen(8080, function () {
console.log(’%s listening at %s’, server.name, server.url);
});
客户端:

var assert = require(‘assert’);
var restify = require(‘restify’);

var client = restify.createJsonClient({
url: ‘http://localhost:8080’,
version: ‘~1.0’
});

client.get(’/echo/mark’, function (err, req, res, obj) {
assert.ifError(err);
console.log(‘Server returned: %j’, obj);
});