What is restify?
Restify is a Node.js web service framework optimized for building RESTful APIs. It is designed to be lightweight and efficient, making it ideal for creating scalable and high-performance web services.
What are restify's main functionalities?
Creating a Basic Server
This code demonstrates how to create a basic Restify server that listens on port 8080 and responds with 'Hello, world!' when a GET request is made to the '/hello' endpoint.
const restify = require('restify');
const server = restify.createServer();
server.get('/hello', (req, res, next) => {
res.send('Hello, world!');
next();
});
server.listen(8080, () => {
console.log('%s listening at %s', server.name, server.url);
});
Middleware Support
This example shows how to use middleware in Restify. The bodyParser plugin is used to parse the body of incoming POST requests, and the server responds with the parsed body.
const restify = require('restify');
const server = restify.createServer();
server.use(restify.plugins.bodyParser());
server.post('/data', (req, res, next) => {
res.send(req.body);
next();
});
server.listen(8080, () => {
console.log('%s listening at %s', server.name, server.url);
});
Error Handling
This code demonstrates how to handle errors in Restify. When a GET request is made to the '/error' endpoint, an InternalServerError is thrown. The 'restifyError' event is used to log the error and send an appropriate response.
const restify = require('restify');
const server = restify.createServer();
server.get('/error', (req, res, next) => {
return next(new restify.errors.InternalServerError('Something went wrong!'));
});
server.on('restifyError', (req, res, err, callback) => {
console.error(err);
res.send(err);
return callback();
});
server.listen(8080, () => {
console.log('%s listening at %s', server.name, server.url);
});
Other packages similar to restify
express
Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. Compared to Restify, Express is more general-purpose and widely used for a variety of web applications, not just RESTful APIs.
hapi
Hapi is a rich framework for building applications and services in Node.js. It is known for its powerful plugin system and configuration-based approach. Hapi offers more built-in features and a different approach to configuration and extensibility compared to Restify.
koa
Koa is a new web framework designed by the team behind Express, aiming to be a smaller, more expressive, and more robust foundation for web applications and APIs. Koa uses async functions to eliminate callback hell and improve error handling, offering a more modern approach compared to Restify.
restify
restify is a smallish framework,
similar to express for building REST APIs. For full
details, see http://restify.com
Join us on IRC at irc.freenode.net
in the #restify
channel for real-time
chat and support. Or see the
Google group.
Usage
Server
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);
});
Client
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);
});
Note that in future versions of restify, the clients have moved to a
separate restify-clients repository
and npm package.
Installation
npm install restify
License
MIT (see "LICENSE" file).
Development
Bugs
See https://github.com/restify/node-restify/issues.
Release process
Here is how to cut a release:
-
Update the version in "package.json" and change ## not yet released
at
the top of "CHANGES.md" to:
## not yet released
## $version
-
Ensure things are correct by running make versioncheck
, then commit
your change, with something like:
git commit -am "$version"
git push origin
-
Run the following to tag and publish the release:
make cutarelease