Restify

11.1.0 · active · verified Sun Apr 19

Restify is an opinionated Node.js web service framework optimized for building semantically correct RESTful APIs. Unlike more generalized frameworks like Express, Restify focuses purely on API development, offering built-in features for introspection, performance, DTrace support, and robust error handling. It's designed for high-throughput, scalable services and is utilized in large-scale Node.js deployments. The current stable version is 11.2.0, released in August 2023. Major releases, often introducing breaking changes or significant feature updates, occur periodically, with more frequent minor and patch updates addressing bugs and adding smaller features.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart sets up a basic Restify server with GET and POST routes, utilizing common plugins for body and query parsing. It includes basic error handling for unmatched routes and general Restify errors, demonstrating fundamental API building blocks.

import * as restify from 'restify';
import { Request, Response, Next } from 'restify';

const server = restify.createServer({
  name: 'MyRestifyApp',
  version: '1.0.0'
});

// Apply common plugins
server.use(restify.plugins.bodyParser()); // Parses application/json, application/x-www-form-urlencoded, multipart/form-data
server.use(restify.plugins.queryParser()); // Parses URL query parameters into req.query

// Define a GET route with a parameter
server.get('/hello/:name', (req: Request, res: Response, next: Next) => {
  res.send({
    message: `Hello, ${req.params.name}!`, // Access route parameters
    query: req.query,
    // body: req.body // Body is not typically present for GET requests
  });
  return next(); // Pass control to the next handler in the chain
});

// Define a POST route to receive data
server.post('/data', (req: Request, res: Response, next: Next) => {
  if (!req.body) {
    res.send(400, { message: 'Request body is required.' });
    return next(false); // Stop the chain if an error occurs
  }
  res.send(201, {
    received: req.body, // Access parsed request body
    status: 'Data processed successfully.'
  });
  return next();
});

// Event listener for unhandled routes (404 Not Found)
server.on('NotFound', (req: Request, res: Response, next: Next) => {
  res.send(404, { message: 'The requested resource was not found.' });
  return next();
});

// Global error handler for Restify errors
server.on('restifyError', (req: Request, res: Response, err: Error, callback: () => void) => {
  console.error(`Unhandled Restify error for ${req.url}:`, err);
  // Optionally modify the error response before sending
  // err.toJSON = () => ({ error: { name: err.name, message: err.message, code: (err as any).statusCode } });
  return callback(); // Continue the error handling chain
});

const port = process.env.PORT ?? 8080;

server.listen(port, () => {
  console.log('%s listening at %s', server.name, server.url); // Server.url is populated after listen()
});

view raw JSON →