{"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.","language":"javascript","status":"abandoned","last_verified":"Wed Apr 22","install":{"commands":["npm install resource-router-middleware"],"cli":null},"imports":["import resource from 'resource-router-middleware';","const resource = require('resource-router-middleware');"],"auth":{"required":false,"env_vars":[]},"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}