{"id":49186,"library":"rest-contracts-express-server","title":"rest-contracts-express-server","description":"A TypeScript library that automatically generates strongly-typed Express route handlers from REST API contracts defined via the rest-contracts package. Version 1.1.3 is the current stable release; no recent updates or defined release cadence. It eliminates manual type mapping by allowing developers to define contracts (method, path, parameters, return type) and then call an implement() method on RestContractsExpressServer, which enforces type safety at compile time and generates Express handlers. Key differentiators: minimal footprint, no code generation step, pure TypeScript compile-time enforcement, and seamless integration with both client and server sides using the same contract definitions. Requires peer dependency rest-contracts@^1.0.8 and Node >=6.9.","status":"active","version":"1.1.3","language":"javascript","source_language":"en","source_url":"https://github.com/UppaJung/rest-contracts","tags":["javascript","TypeScript","REST","API","AJAX","rest-contracts","restful","Contract","Types","typescript"],"install":[{"cmd":"npm install rest-contracts-express-server","lang":"bash","label":"npm"},{"cmd":"yarn add rest-contracts-express-server","lang":"bash","label":"yarn"},{"cmd":"pnpm add rest-contracts-express-server","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency required for defining API contracts; the express server consumes contracts exported by rest-contracts.","package":"rest-contracts","optional":false}],"imports":[{"note":"ESM import is preferred; CommonJS require works but the library ships TypeScript types, so use named import.","wrong":"const RestContractsExpressServer = require('rest-contracts-express-server')","symbol":"RestContractsExpressServer","correct":"import { RestContractsExpressServer } from 'rest-contracts-express-server'"},{"note":"Default import is also valid since the package exports a default export; avoid mixing named and default.","wrong":"const RestContractsExpressServer = require('rest-contracts-express-server').RestContractsExpressServer","symbol":"RestContractsExpressServer","correct":"import RestContractsExpressServer from 'rest-contracts-express-server'"},{"note":"CommonJS destructured require works; plain require returns the module object. Use destructuring.","wrong":"const RestContractsExpressServer = require('rest-contracts-express-server')","symbol":"RestContractsExpressServer","correct":"const { RestContractsExpressServer } = require('rest-contracts-express-server')"}],"quickstart":{"code":"import * as express from 'express';\nimport { RestContractsExpressServer } from 'rest-contracts-express-server';\nimport { API } from 'rest-contracts';\n\n// Define a contract\nconst GetUser = API.Get\n  .Path('/users/:id/')\n  .PathParameters<{ id: string }>()\n  .Returns<{ name: string; email: string }>();\n\n// Create Express app and router\nconst app = express();\nconst router = express.Router();\n\n// Create server instance\nconst server = new RestContractsExpressServer(router);\n\n// Implement the contract\nserver.implement(GetUser, (params) => {\n  // params is typed as { id: string }\n  return { name: 'John Doe', email: 'john@example.com' };\n});\n\napp.use('/api', router);\napp.listen(3000);","lang":"typescript","description":"Demonstrates defining a REST contract with rest-contracts and implementing it with rest-contracts-express-server, showing automatic type safety for path parameters and return value."},"warnings":[{"fix":"Consider migrating to a more actively maintained alternative like rest-contracts-lambda or implementing your own server adapter.","message":"The package rest-contracts-express-server is deprecated in favor of rest-contracts-express-server v2? (Not specified, but the GitHub repo recommends using rest-contracts with other server packages).","severity":"deprecated","affected_versions":"1.x"},{"fix":"Always create an express.Router() and pass it to the constructor, then mount the router on the app.","message":"Requires Express.Router(), not Express application instance directly. Passing app instead of router will cause runtime errors.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Pass the same router object to all RestContractsExpressServer instances or reuse the same server instance.","message":"Must use the same router instance for all implement() calls; otherwise routes may not register correctly.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"search_vec":"'1.0.8':112 '1.1.3':30 '6.9':115 'ajax':120 'allow':49 'api':20,119 'automat':10 'cadenc':42 'call':61 'client':97 'code':83 'compil':72,89 'compile-tim':88 'contract':3,21,27,53,104,111,123,125 'current':33 'defin':22,40,52 'definit':105 'depend':108 'develop':50 'differenti':79 'elimin':44 'enforc':68,91 'express':4,15,76 'footprint':81 'generat':11,75,84 'handler':17,77 'implement':63 'integr':94 'javascript':116 'key':78 'librari':8 'manual':45 'map':47 'method':54,64 'minim':80 'node':114 'packag':28 'paramet':56 'path':55 'peer':107 'pure':86 'recent':37 'releas':35,41 'requir':106 'rest':2,19,26,110,118,122,124 'rest-contract':25,109,121 'rest-contracts-express-serv':1 'restcontractsexpressserv':66 'return':57 'rout':16 'safeti':70 'seamless':93 'server':5,99 'side':100 'stabl':34 'step':85 'strong':13 'strongly-typ':12 'time':73,90 'type':14,46,58,69,126 'typescript':7,87,117,127 'updat':38 'use':101 'version':29 'via':23","created_at":"2026-06-07T17:00:29.855143+00:00","updated_at":"2026-06-07T17:00:29.855143+00:00","problems":[{"fix":"Ensure the contract is properly defined and imported before calling implement(contract, handler).","cause":"Passing an undefined or null contract to implement()","error":"TypeError: Cannot read properties of undefined (reading 'path')"},{"fix":"Check that the handler's return type matches the Returns<> type in the contract.","cause":"Mismatched types between contract and handler – e.g., handler returns wrong shape","error":"TypeScript error: Argument of type '...' is not assignable to parameter of type '...'"},{"fix":"Ensure app.use('/basepath', router) is called after all implement() calls.","cause":"The router is not mounted on the Express app or the base path is wrong","error":"Error: Route not found"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/UppaJung/rest-contracts","github":"https://github.com/UppaJung/rest-contracts","docs":null,"changelog":null,"pypi":null,"npm":"rest-contracts-express-server","openapi_spec":null,"status_page":null,"smithery":null,"categories":["server"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-07","next_check":"2026-09-05","install_tag":null}}