{"id":19450493,"url":"https://github.com/mswjs/http-middleware","last_synced_at":"2025-04-05T19:09:05.780Z","repository":{"id":38822141,"uuid":"387597818","full_name":"mswjs/http-middleware","owner":"mswjs","description":"Spawn an HTTP server from your request handlers or apply them to an existing server using a middleware.","archived":false,"fork":false,"pushed_at":"2024-04-24T23:10:31.000Z","size":228,"stargazers_count":96,"open_issues_count":4,"forks_count":12,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-05-01T11:39:23.966Z","etag":null,"topics":["express","handler","http","middleware","msw","request","server"],"latest_commit_sha":null,"homepage":"https://npm.im/@mswjs/http-middleware","language":"TypeScript","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/mswjs.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":"2021-07-19T21:25:14.000Z","updated_at":"2024-06-18T17:09:43.122Z","dependencies_parsed_at":"2024-06-18T17:09:28.356Z","dependency_job_id":"52ec131f-56a8-4b0c-8e94-5ac796f6ed75","html_url":"https://github.com/mswjs/http-middleware","commit_stats":{"total_commits":50,"total_committers":9,"mean_commits":5.555555555555555,"dds":"0.31999999999999995","last_synced_commit":"dfd8c682696f47f3242c6f5a33ab9077a656c5b5"},"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mswjs%2Fhttp-middleware","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mswjs%2Fhttp-middleware/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mswjs%2Fhttp-middleware/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mswjs%2Fhttp-middleware/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mswjs","download_url":"https://codeload.github.com/mswjs/http-middleware/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247386263,"owners_count":20930618,"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":["express","handler","http","middleware","msw","request","server"],"created_at":"2024-11-10T16:38:01.965Z","updated_at":"2025-04-05T19:09:05.751Z","avatar_url":"https://github.com/mswjs.png","language":"TypeScript","readme":"# `@mswjs/http-middleware`\n\nSpawn an [Express](https://expressjs.com) server from your [Mock Service Worker](https://github.com/mswjs/msw) request handlers or apply them to an existing server using a middleware.\n\n## When to use this?\n\nYou should always prefer Mock Service Worker for API mocking because it can meet most of your requirements without having to spawn and maintain an actual HTTP server. Please refer to the [Getting started](https://mswjs.io/docs/getting-started) tutorial to integrate next-generation API mocking into your application.\n\nThere are, however, use cases when this extension can be applicable:\n\n- If you wish to `curl` your mock definitions locally;\n- When prototyping a Node.js backend implementation;\n- When integrating API mocking in a complex application architecture (i.e. for dockerized applications).\n\n## Getting started\n\n### Install\n\n```sh\n$ npm install @mswjs/http-middleware\n```\n\n### Declare request handlers\n\n```js\n// src/mocks/handlers.js\nimport { http, graphql, HttpResponse } from 'msw'\n\nexport const handlers = [\n  http.post('/user', () =\u003e {\n    return HttpResponse.json({ firstName: 'John' })\n  }),\n  graphql.query('GetUser', () =\u003e {\n    return HttpResponse.json({\n      data: {\n        user: {\n          firstName: 'John',\n        },\n      },\n    })\n  }),\n]\n```\n\n\u003e Learn more about writing [request handlers](https://mswjs.io/docs/concepts/request-handler).\n\n### Integration\n\n#### Option 1: Standalone server\n\n```js\nimport { createServer } from '@mswjs/http-middleware'\nimport { handlers } from './handlers'\n\nconst httpServer = createServer(...handlers)\n\nhttpServer.listen(9090)\n```\n\n#### Option 2: Middleware\n\n```js\nimport { createMiddleware } from '@mswjs/http-middleware'\nimport app from './app'\nimport { handlers } from './handlers'\n\napp.use(createMiddleware(...handlers))\n```\n\n## API\n\n### `createServer(...handlers: RequestHandler[])`\n\nEstablishes a standalone Express server that uses the given request handlers to process all incoming requests.\n\n```ts\nimport { http, HttpResponse } from 'msw'\nimport { createServer } from '@mswjs/http-middleware'\n\nconst httpServer = createServer(\n  http.get('/user', () =\u003e {\n    return HttpResponse.json({ firstName: 'John' })\n  }),\n)\n\nhttpServer.listen(9090)\n```\n\nMaking a `GET http://localhost:9090/user` request returns the following response:\n\n```sh\n200 OK\nContent-Type: application/json\n\n{\n  \"firstName\": \"John\"\n}\n```\n\n### `createMiddleware(...handlers: RequestHandler[])`\n\nCreates an Express middleware function that uses the given request handlers to process all incoming requests.\n\n```ts\nimport { http, HttpResponse } from 'msw'\nimport { createMiddleware } from '@mswjs/http-middleware'\n\nconst app = express()\n\napp.use(\n  createMiddleware(\n    http.get('/user', () =\u003e {\n      return HttpResponse.json({ firstName: 'John' })\n    }),\n  ),\n)\n\napp.use(9090)\n```\n\n## Mentions\n\n- [David Idol](https://github.com/idolize), original implementation.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmswjs%2Fhttp-middleware","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmswjs%2Fhttp-middleware","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmswjs%2Fhttp-middleware/lists"}