{"id":18451491,"url":"https://github.com/trasherdk/example-express-api-best-practices","last_synced_at":"2025-06-22T22:04:25.531Z","repository":{"id":103287042,"uuid":"294606159","full_name":"trasherdk/example-express-api-best-practices","owner":"trasherdk","description":"This is an example app created for the blog post '5 best practices for building a modern API with Express'.","archived":false,"fork":false,"pushed_at":"2022-12-13T14:18:20.000Z","size":51,"stargazers_count":0,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-20T00:55:17.440Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/trasherdk.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-09-11T05:50:24.000Z","updated_at":"2021-06-01T04:24:46.000Z","dependencies_parsed_at":null,"dependency_job_id":"57386d95-55cd-433f-9645-ff864dadd48b","html_url":"https://github.com/trasherdk/example-express-api-best-practices","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/trasherdk/example-express-api-best-practices","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trasherdk%2Fexample-express-api-best-practices","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trasherdk%2Fexample-express-api-best-practices/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trasherdk%2Fexample-express-api-best-practices/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trasherdk%2Fexample-express-api-best-practices/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/trasherdk","download_url":"https://codeload.github.com/trasherdk/example-express-api-best-practices/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trasherdk%2Fexample-express-api-best-practices/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261374407,"owners_count":23148975,"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":[],"created_at":"2024-11-06T07:28:50.559Z","updated_at":"2025-06-22T22:04:20.519Z","avatar_url":"https://github.com/trasherdk.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Express API Best Practices Example Application\n\n\u003e This is an example Express API application. It combines techniques from the blog posts\n'[5 best practices for building a modern API with Express](https://simonplend.com/5-best-practices-for-building-a-modern-api-with-express/)' and '[Send awesome structured error responses with Express](https://simonplend.com/send-awesome-structured-error-responses-with-express/)'.\n\n_Note: A real application should have plenty of tests so that you can have confidence\nin what you're releasing. This application does not have tests as it's been been\nput together to demonstrate the best practices described [in my blog post](https://simonplend.com/5-best-practices-for-building-a-modern-api-with-express/),\nand is not intended to be released in a production environment._\n\n## Requirements\n\n- Node.js \u003e= v12\n\n## Usage\n\n```bash\n# Install application dependencies\nnpm install\n\n# Run API server\nnpm start\n```\n\n## Making API requests\n\n- You can make `POST` requests to http://localhost:3000/user\n- You can make `GET`, `PUT` and `DELETE` requests to http://localhost:3000/user/1234\n\nTry an invalid `POST` request to create a new user:\n\n```bash\ncurl -v -X POST \\\n  -H 'Content-Type: application/json' \\\n  -d '{ \"first_name\": \"Rosa\", \"last_name\": \"Parks\" }' \\\n  http://localhost:3000/user\n```\n\n_Note: No data sent to the API is stored by this example application._\n\nTry a `GET` request with an invalid user ID:\n\n```bash\ncurl -v http://localhost:3000/user/abc\n```\n\n## Best practices\n\nThe five best practices which are described in my\n[Express best practices blog post](https://simonplend.com/5-best-practices-for-building-a-modern-api-with-express/)\nand applied in this example application are:\n\n1. Enable the full use of `async` and `await`\n2. Validate request data with JSON Schema\n3. Use an existing format for error responses\n4. Send CORS response headers so web pages can call your API\n5. Separate your concerns\n\nThis application also implements the techniques for sending \"problem details\"\nerror responses which are described in my '[Send awesome structured error responses with Express](https://simonplend.com/send-awesome-structured-error-responses-with-express/)' blog post.\n\n## Libraries and frameworks used\n\n- [Express](https://expressjs.com/) - \"Fast, unopinionated, minimalist web\n  framework for Node.js\".\n- [express-async-errors](https://github.com/davidbanham/express-async-errors) -\n  \"A dead simple ES6 async/await support hack for ExpressJS\"\n- [express-json-validator-middleware](https://github.com/vacekj/express-json-validator-middleware) -\n  \"express.js middleware for validating requests against JSON Schema\"\n- [body-parser](https://github.com/expressjs/body-parser) - \"Node.js body parsing middleware\"\n- [cors](https://github.com/expressjs/cors) - \"CORS is a node.js package for\n  providing a Connect/Express middleware that can be used to enable CORS with various options.\"\n- [http-errors](https://www.npmjs.com/package/http-errors) - \"Create HTTP errors for Express, Koa, Connect, etc. with ease.\"\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrasherdk%2Fexample-express-api-best-practices","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftrasherdk%2Fexample-express-api-best-practices","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrasherdk%2Fexample-express-api-best-practices/lists"}