{"library":"restify","title":"Restify","description":"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.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install restify"],"cli":null},"imports":["import * as restify from 'restify';\nconst server = restify.createServer({ name: 'MyApi' });","import * as restify from 'restify';\nserver.use(restify.plugins.bodyParser());","import { Request, Response, Next } from 'restify';","const restify = require('restify');\nconst server = restify.createServer();"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import * as restify from 'restify';\nimport { Request, Response, Next } from 'restify';\n\nconst server = restify.createServer({\n  name: 'MyRestifyApp',\n  version: '1.0.0'\n});\n\n// Apply common plugins\nserver.use(restify.plugins.bodyParser()); // Parses application/json, application/x-www-form-urlencoded, multipart/form-data\nserver.use(restify.plugins.queryParser()); // Parses URL query parameters into req.query\n\n// Define a GET route with a parameter\nserver.get('/hello/:name', (req: Request, res: Response, next: Next) => {\n  res.send({\n    message: `Hello, ${req.params.name}!`, // Access route parameters\n    query: req.query,\n    // body: req.body // Body is not typically present for GET requests\n  });\n  return next(); // Pass control to the next handler in the chain\n});\n\n// Define a POST route to receive data\nserver.post('/data', (req: Request, res: Response, next: Next) => {\n  if (!req.body) {\n    res.send(400, { message: 'Request body is required.' });\n    return next(false); // Stop the chain if an error occurs\n  }\n  res.send(201, {\n    received: req.body, // Access parsed request body\n    status: 'Data processed successfully.'\n  });\n  return next();\n});\n\n// Event listener for unhandled routes (404 Not Found)\nserver.on('NotFound', (req: Request, res: Response, next: Next) => {\n  res.send(404, { message: 'The requested resource was not found.' });\n  return next();\n});\n\n// Global error handler for Restify errors\nserver.on('restifyError', (req: Request, res: Response, err: Error, callback: () => void) => {\n  console.error(`Unhandled Restify error for ${req.url}:`, err);\n  // Optionally modify the error response before sending\n  // err.toJSON = () => ({ error: { name: err.name, message: err.message, code: (err as any).statusCode } });\n  return callback(); // Continue the error handling chain\n});\n\nconst port = process.env.PORT ?? 8080;\n\nserver.listen(port, () => {\n  console.log('%s listening at %s', server.name, server.url); // Server.url is populated after listen()\n});","lang":"typescript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}