{"id":15190645,"url":"https://github.com/deej4y/emfrest","last_synced_at":"2026-03-03T16:07:18.573Z","repository":{"id":57686084,"uuid":"477051489","full_name":"DEEJ4Y/emfrest","owner":"DEEJ4Y","description":"Quickly build RESTful APIs with Express and Mongoose.","archived":false,"fork":false,"pushed_at":"2023-01-20T16:28:49.000Z","size":1276,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-11T02:17:59.474Z","etag":null,"topics":["api","api-rest","emfrest","express","expressjs","mongodb","mongoose","mongoosejs","nodejs","rest","rest-api","restful-api"],"latest_commit_sha":null,"homepage":"https://deej4y.github.io/emfrest/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DEEJ4Y.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-04-02T12:42:55.000Z","updated_at":"2022-04-09T04:04:11.000Z","dependencies_parsed_at":"2023-02-12T03:31:24.756Z","dependency_job_id":null,"html_url":"https://github.com/DEEJ4Y/emfrest","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DEEJ4Y%2Femfrest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DEEJ4Y%2Femfrest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DEEJ4Y%2Femfrest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DEEJ4Y%2Femfrest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DEEJ4Y","download_url":"https://codeload.github.com/DEEJ4Y/emfrest/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241110785,"owners_count":19911396,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","api-rest","emfrest","express","expressjs","mongodb","mongoose","mongoosejs","nodejs","rest","rest-api","restful-api"],"created_at":"2024-09-27T20:43:20.455Z","updated_at":"2026-03-03T16:07:18.539Z","avatar_url":"https://github.com/DEEJ4Y.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Express Mongoose Fast REST API\n\nQuickly build RESTful APIs with Express and Mongoose.\n\n[Documentation](https://deej4y.github.io/emfrest/)\n\n[Github](https://github.com/DEEJ4Y/emfrest)\n\n[npm](https://www.npmjs.com/package/emfrest)\n\nHave a look at the list of [service and utility functions](https://deej4y.github.io/emfrest/list_namespace.html) and other [middleware and global functions](https://deej4y.github.io/emfrest/global.html#methods).\n\n## Installation\n\n```sh\nnpm i emfrest\n```\n\nThis also installs express and mongoose, so you can dive right into making your API.\n\n## Usage\n\n### Basic\n\nCreate an API using `Api`.\n\n```js\nconst { Api } = require(\"emfrest\");\n\nApi(app, { model: Book, modelName: \"book\" });\n```\n\nAdd your mongoose model as `model`, the example uses a model called `Book`.\n\nAlso give it a `modelName`. The example uses `modelName` as `book`. This creates your routes at `/books`. Make sure this is singular and unique among your routes.\n\nFor every `Api` you create, you will get the following routes:\n\n| Method | Route          |\n| :----- | :------------- |\n| GET    | /books         |\n| POST   | /books         |\n| GET    | /books/:bookId |\n| PUT    | /books/:bookId |\n| DELETE | /books/:bookId |\n\n#### Adding endpoint specific middleware\n\nYou can also have endpoint specific middleware. The `preMiddleware` option takes an array of Middlewares that will run before the controller function.\n\n```js\nApi(app, {\n  model: Book,\n  modelName: \"book\",\n  preMiddleware: [\n    protect,\n    myMiddlewareFunction1,\n    myMiddlewareFunction2 /*...*/,\n  ],\n});\n```\n\n#### Route prefix for an endpoint\n\nIf you want to serve the API from a subpath, you can use the `routePrefix` option.\n\n```js\nconst { Api } = require(\"emfrest\");\n\nApi(app, { model: Book, modelName: \"book\", routePrefix: \"/v1\" });\n```\n\nThis would result in the following routes\n\n| Method | Route             |\n| :----- | :---------------- |\n| GET    | /v1/books         |\n| POST   | /v1/books         |\n| GET    | /v1/books/:bookId |\n| PUT    | /v1/books/:bookId |\n| DELETE | /v1/books/:bookId |\n\n### Add to an existing app\n\nNote: App should be connected to a database. App should also be able to read json data from requests (`body-parser` or `express.json()`).\n\n1. Require `Api` and `errorHandler` from `emfrest`\n\n   ```js\n   const { Api, errorHandler } = require(\"emfrest\");\n   ```\n\n2. Add the `errorHandler` middleware at the end of your app, before `app.listen()`.\n\n   ```js\n   app.use(errorHandler);\n   ```\n\n3. Create an API using `Api`.\n\n   ```js\n   Api(app, { model: Book, modelName: \"book\" });\n   ```\n\n   Add your mongoose model as `model`, the example uses a model called `Book`.\n\n   Also give it a `modelName`. The example uses `modelName` as `book`. This creates your routes at `/books`. Make sure this is singular and unique among your routes.\n\n4. Start your server. It should show the following line on startup\n\n   ```plain\n   Initialized api for book at /books\n   ```\n\n### From scratch\n\n1. Create a file `server.js`.\n2. Add your express boilerplate code.\n\n   ```js\n   const express = require(\"express\");\n\n   const app = express();\n\n   app.use(express.json());\n\n   app.get(\"/\", (req, res) =\u003e {\n     res.json({ success: true, message: \"Check your api endpoint\" });\n   });\n\n   const PORT = process.env.PORT || 3000;\n   const server = app.listen(PORT, () =\u003e\n     console.log(`Server started on port ${PORT}`)\n   );\n   ```\n\n3. Connect to MongoDB\n\n   ```js\n   const {\n     Api,\n     connectDB,\n     errorHandler,\n     handlePromiseRejections,\n   } = require(\"emfrest\");\n\n   connectDB(\"mongodb://localhost:27017/library\");\n   ```\n\n   You can use a connection string of your choice.\n\n   We will use `Api`, `errorHandler` and `handlePromiseRejections` later.\n\n4. Create your schema and mongoose model.\n\n   ```js\n   const mongoose = require(\"mongoose\");\n\n   const BookSchema = new mongoose.Schema({\n     name: {\n       type: String,\n     },\n     description: {\n       type: String,\n     },\n   });\n\n   const Book = mongoose.model(\"Book\", BookSchema);\n   ```\n\n5. Add the `errorHandler` middleware at the end of your app, before `app.listen()`.\n\n   ```js\n   app.use(errorHandler);\n   ```\n\n6. Create an API using `Api`.\n\n   ```js\n   Api(app, { model: Book, modelName: \"book\" });\n   ```\n\n   Add your mongoose model as `model`, the example uses a model called `Book`.\n\n   Also give it a `modelName`. The example uses `modelName` as `book`. This creates your routes at `/books`. Make sure this is singular and unique among your routes.\n\n7. Handle promise rejections\n\n   ```js\n   handlePromiseRejections(server);\n   ```\n\nYour code should now look like this:\n\n```js\nconst express = require(\"express\");\nconst {\n  Api,\n  connectDB,\n  errorHandler,\n  handlePromiseRejections,\n} = require(\"emfrest\");\n\nconnectDB(\"mongodb://localhost:27017/library\");\n\nconst mongoose = require(\"mongoose\");\n\nconst BookSchema = new mongoose.Schema({\n  name: {\n    type: String,\n  },\n  description: {\n    type: String,\n  },\n  createdAt: {\n    type: Date,\n    default: Date.now,\n  },\n});\n\nconst Book = mongoose.model(\"Book\", BookSchema);\n\nconst app = express();\n\napp.use(express.json());\n\nApi(app, { model: Book, modelName: \"book\" });\n\napp.get(\"/\", (req, res) =\u003e {\n  res.json({ success: true, message: \"Check your api endpoint\" });\n});\n\napp.use(errorHandler);\n\nconst PORT = process.env.PORT || 3000;\nconst server = app.listen(PORT, () =\u003e\n  console.log(`Server started on port ${PORT}`)\n);\n\nhandlePromiseRejections(server);\n```\n\n## Reusable Functions\n\n### Services\n\nService functions are used to make calls to your MongoDB database using your mongoose model.\n\n1. [Get All documents of a model with a query(optional).](https://deej4y.github.io/emfrest/services.html#.exports.getAllService)\n2. [Create a document.](https://deej4y.github.io/emfrest/services.html#.exports.createResourceService)\n3. [Get a document by its ObjectId.](https://deej4y.github.io/emfrest/services.html#.exports.getOneByIdService)\n4. [Update a document by its ObjectId.](https://deej4y.github.io/emfrest/services.html#.exports.updateOneByIdService)\n5. [Delete a document by its ObjectId.](https://deej4y.github.io/emfrest/services.html#.exports.deleteByIdService)\n\n### Utilities\n\nUtility functions to code faster\n\n1. [Create an error object with a message and http status code](https://deej4y.github.io/emfrest/utils.html#exports.ErrorResponse).\n2. [Connect to a MongoDB database](https://deej4y.github.io/emfrest/utils.html#.exports.connectDB).\n3. [Promise rejection handler](https://deej4y.github.io/emfrest/utils.html#.exports.handlePromiseRejections).\n\n## Liked emfrest?\n\nEnjoyed making your APIs faster? Star [efmrest](https://github.com/DEEJ4Y/emfrest) on [Github](https://github.com/DEEJ4Y/emfrest).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeej4y%2Femfrest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdeej4y%2Femfrest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeej4y%2Femfrest/lists"}