{"id":18764,"library":"router-middleware","title":"router-middleware","description":"An Express-style router for Node.js with full TypeScript safety, including inferred route params from path strings (e.g., '/user/:id' gives req.params.id: string) and typed request/response bodies via generic parameters. Current stable version is 6.1.6. Key differentiators: no view engine, no global state, dual ESM/CJS exports, built-in error handling, and safe defaults (1 MB JSON limit, strict content-type checks). Released actively with regular updates; ships its own TypeScript types.","status":"active","version":"6.1.6","language":"javascript","source_language":"en","source_url":null,"tags":["javascript","router","middleware","typescript","express","nodejs","http server"],"install":[{"cmd":"npm install router-middleware","lang":"bash","label":"npm"},{"cmd":"yarn add router-middleware","lang":"bash","label":"yarn"},{"cmd":"pnpm add router-middleware","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"ESM default export. CommonJS users must use `require('router-middleware').default`.","wrong":"const createApp = require('router-middleware');","symbol":"default (createApp)","correct":"import createApp from 'router-middleware';"},{"note":"Named export available for both ESM and CJS.","wrong":"const { Router } = require('router-middleware');","symbol":"Router","correct":"import { Router } from 'router-middleware';"},{"note":"CommonJS requires explicit `.default` due to ESM-first package.","wrong":"const createApp = require('router-middleware');","symbol":"default (require)","correct":"const createApp = require('router-middleware').default;"}],"quickstart":{"code":"import http from 'node:http';\nimport createApp from 'router-middleware';\n\nconst app = createApp();\nconst server = http.createServer(app);\n\napp.use(app.jsonParser());\n\napp.get<'/user/:id'>('/user/:id', (req, res) => {\n  res.json({ id: req.params.id });\n});\n\napp.post<'/user/:id/email', { email: string }, { ok: true; id: string; email: string }>(\n  '/user/:id/email',\n  (req, res) => {\n    res.status(201).json({\n      ok: true,\n      id: req.params.id,\n      email: req.body.email,\n    });\n  }\n);\n\napp.get<'/health'>('/health', (_req, res) => {\n  res.status(200).send('OK');\n});\n\napp.useError((err, _req, res, _next) => {\n  res.status(err?.statusCode ?? 500).json({ error: err?.message ?? 'error' });\n});\n\nserver.listen(5150);","lang":"typescript","description":"Creates a typed HTTP server with route param inference and typed request/response bodies."},"warnings":[{"fix":"Use `const app = require('router-middleware').default;` instead of `const app = require('router-middleware');`","message":"Default export is ESM-only; CJS require must use .default","severity":"breaking","affected_versions":">=6.0.0"},{"fix":"Replace `req.param('id')` with `req.params.id`","message":"Changed from `req.param` to `req.params` for route parameters","severity":"breaking","affected_versions":">=5.0.0"},{"fix":"Add `app.use(app.jsonParser());` before routes that expect JSON body","message":"JSON body parser is not automatic; must be explicitly added with app.use(app.jsonParser())","severity":"gotcha","affected_versions":">=6.0.0"},{"fix":"Use `res.json({...})` instead of `res.send({...})`","message":"res.send() does NOT accept objects; use res.json() for objects","severity":"gotcha","affected_versions":">=6.0.0"},{"fix":"Migrate to `app.onError(handler)` when upgrading to v7","message":"app.useError() has been renamed to app.onError() in latest v7 beta","severity":"deprecated","affected_versions":"7.0.0-beta"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Change to const app = require('router-middleware').default;","cause":"Incorrect import: using require('router-middleware') without .default in CJS","error":"TypeError: app is not a function"},{"fix":"Replace res.send(obj) with res.json(obj)","cause":"Using res.send() with an object instead of res.json()","error":"Type '{}' is not assignable to type 'string'"},{"fix":"Ensure `const app = createApp();` is called and `app.use(app.jsonParser());` is added","cause":"Missing app.jsonParser() call or forgot to call createApp()","error":"Cannot find name 'app'."},{"fix":"Add generic type like app.get<'/path/:id'>('/path/:id', handler) to infer params","cause":"Route handler not using generic parameter for path, so params is not typed","error":"Property 'params' does not exist on type 'Request'"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}