{"id":17466,"library":"resource-router-middleware","title":"Express Resource Router Middleware","description":"resource-router-middleware provides a concise, declarative way to define RESTful API endpoints for Express applications. It allows developers to configure a full CRUD (Create, Read, Update, Delete) resource using a single object literal, mapping HTTP methods (GET, POST, PUT, DELETE) to corresponding handler functions (list, create, read, update, delete). The current stable version is 0.7.0, released in 2017. The project appears to be unmaintained, with its last commit in 2020, suggesting an abandoned release cadence. Its key differentiator is simplifying the definition of standard REST resources, abstracting away individual route declarations and integrating with Express's parameter loading mechanisms, which can lead to more readable and organized route definitions compared to manual `app.get()`, `app.post()` etc.","status":"abandoned","version":"0.7.0","language":"javascript","source_language":"en","source_url":"https://github.com/developit/resource-router-middleware","tags":["javascript"],"install":[{"cmd":"npm install resource-router-middleware","lang":"bash","label":"npm"},{"cmd":"yarn add resource-router-middleware","lang":"bash","label":"yarn"},{"cmd":"pnpm add resource-router-middleware","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency, required for the middleware to function within an Express application.","package":"express","optional":false}],"imports":[{"note":"The package exports its main function as a default export.","wrong":"import { resource } from 'resource-router-middleware';","symbol":"resource","correct":"import resource from 'resource-router-middleware';"},{"note":"CommonJS `require` is fully supported for older Node.js environments and build setups.","symbol":"resource","correct":"const resource = require('resource-router-middleware');"}],"quickstart":{"code":"import express from 'express';\nimport resource from 'resource-router-middleware';\n\nconst app = express();\napp.use(express.json()); // For parsing application/json\n\nconst users = [];\n\n// A simple mock for `users` data\nfor (let i = 0; i < 3; i++) {\n  users.push({ id: String(i), name: `User ${i}`, email: `user${i}@example.com` });\n}\n\napp.use('/users', resource({\n  mergeParams: true,\n  id: 'user',\n\n  load(req, id, callback) {\n    const user = users.find(u => u.id === id);\n    const err = user ? null : 'Not found';\n    callback(err, user);\n  },\n\n  list({ params }, res) {\n    res.json(users);\n  },\n\n  create({ body }, res) {\n    const newUser = { ...body, id: String(users.length) };\n    users.push(newUser);\n    res.status(201).json(newUser);\n  },\n\n  read({ user }, res) {\n    res.json(user);\n  },\n\n  update({ user, body }, res) {\n    const index = users.indexOf(user);\n    if (index !== -1) {\n      for (let key in body) {\n        if (key !== 'id') {\n          user[key] = body[key];\n        }\n      }\n      users[index] = user;\n      res.status(204).send();\n    } else {\n      res.status(404).send('Not Found');\n    }\n  },\n\n  delete({ user }, res) {\n    const index = users.indexOf(user);\n    if (index !== -1) {\n      users.splice(index, 1);\n      res.status(204).send();\n    } else {\n      res.status(404).send('Not Found');\n    }\n  }\n}));\n\napp.get('/', (req, res) => res.send('Welcome to the API! Try /users or /users/0'));\n\nconst PORT = process.env.PORT || 3000;\napp.listen(PORT, () => {\n  console.log(`Server running on http://localhost:${PORT}`);\n  console.log('Available endpoints: /users (GET, POST), /users/:id (GET, PUT, DELETE)');\n});\n","lang":"javascript","description":"This example sets up an Express application with a `/users` RESTful resource using `resource-router-middleware`. It demonstrates defining all CRUD operations for a 'user' entity, including data loading and error handling. It shows how to use `express.json()` for request body parsing and how to start the server."},"warnings":[{"fix":"Consider alternatives like `express.Router()` with manual route definitions or more actively maintained REST frameworks if starting a new project. For existing projects, proceed with caution and thorough testing.","message":"The project appears to be abandoned since its last release in 2017 (v0.7.0) and last commit in 2020. It's not actively maintained and may not be compatible with recent versions of Node.js or Express without issues.","severity":"deprecated","affected_versions":">=0.7.0"},{"fix":"Ensure consistent error handling throughout your Express application. If using async middleware elsewhere, be mindful of how errors from this callback-style `load` function are caught and processed. You might need to wrap the `load` function or adapt other parts of your app.","message":"The `load` function uses a Node.js-style callback (`callback(err, data)`). Modern Express middleware often uses Promises or `async/await`. Mixing these patterns without careful handling can lead to difficult-to-debug issues, especially with error propagation.","severity":"gotcha","affected_versions":">=0.7.0"},{"fix":"For TypeScript projects, you'll likely need to extend the `Express.Request` interface to declare properties like `user` that are dynamically added by the `load` function. For example: `declare namespace Express { interface Request { user?: any; } }`.","message":"The package's use of `req.params[this.id]` and direct manipulation of `req` properties (`req.user`) might conflict with TypeScript or stricter linting rules without proper declaration merging for `Request` types.","severity":"gotcha","affected_versions":">=0.7.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Ensure that `resource()` is called and its return value (which is an Express Router) is passed to `app.use()` or `app.route()`. The quickstart example demonstrates this correctly: `app.use('/users', resource(...));`.","cause":"Attempting to pass the result of `resource()` directly to `app.use()` without mounting it as a router.","error":"TypeError: app.use() requires a middleware function but got a Object"},{"fix":"Verify that your `list`, `create`, `read`, `update`, and `delete` handlers, as well as the `load` function's error callback, send only one response per request. If `callback('error')` is invoked, subsequent `res.json()` calls will fail. Ensure explicit `return` statements after sending a response or calling `next()`.","cause":"Sending multiple responses within a single request handler, or trying to send a response after `next()` has been called and another handler has already sent a response.","error":"Error: Can't set headers after they are sent to the client."}],"ecosystem":"npm","meta_description":null}