Installations
npm install @mswjs/http-middleware
Developer Guide
Typescript
No
Module System
CommonJS
Min. Node Version
>=18
Node Version
18.20.5
NPM Version
10.8.2
Score
69.5
Supply Chain
89.7
Quality
81.8
Maintenance
100
Vulnerability
99.3
License
Releases
Contributors
Languages
TypeScript (95.43%)
JavaScript (4.57%)
Developer
Download Statistics
Total Downloads
1,941,128
Last Day
4,536
Last Week
22,581
Last Month
114,001
Last Year
1,131,802
GitHub Statistics
115 Stars
60 Commits
13 Forks
4 Watching
1 Branches
15 Contributors
Bundle Size
585.56 kB
Minified
233.72 kB
Minified + Gzipped
Package Meta Information
Latest Version
0.10.3
Package Id
@mswjs/http-middleware@0.10.3
Unpacked Size
10.20 kB
Size
3.73 kB
File Count
8
NPM Version
10.8.2
Node Version
18.20.5
Publised On
12 Jan 2025
Total Downloads
Cumulative downloads
Total Downloads
1,941,128
Last day
-18.1%
4,536
Compared to previous day
Last week
-26.2%
22,581
Compared to previous week
Last month
-3.4%
114,001
Compared to previous month
Last year
111.8%
1,131,802
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
@mswjs/http-middleware
Spawn an Express server from your Mock Service Worker request handlers or apply them to an existing server using a middleware.
When to use this?
You 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 tutorial to integrate next-generation API mocking into your application.
There are, however, use cases when this extension can be applicable:
- If you wish to
curl
your mock definitions locally; - When prototyping a Node.js backend implementation;
- When integrating API mocking in a complex application architecture (i.e. for dockerized applications).
Getting started
Install
1$ npm install @mswjs/http-middleware
Declare request handlers
1// src/mocks/handlers.js 2import { http, graphql, HttpResponse } from 'msw' 3 4export const handlers = [ 5 http.post('/user', () => { 6 return HttpResponse.json({ firstName: 'John' }) 7 }), 8 graphql.query('GetUser', () => { 9 return HttpResponse.json({ 10 data: { 11 user: { 12 firstName: 'John', 13 }, 14 }, 15 }) 16 }), 17]
Learn more about writing request handlers.
Integration
Option 1: Standalone server
1import { createServer } from '@mswjs/http-middleware' 2import { handlers } from './handlers' 3 4const httpServer = createServer(...handlers) 5 6httpServer.listen(9090)
Option 2: Middleware
1import { createMiddleware } from '@mswjs/http-middleware' 2import app from './app' 3import { handlers } from './handlers' 4 5app.use(createMiddleware(...handlers))
API
createServer(...handlers: RequestHandler[])
Establishes a standalone Express server that uses the given request handlers to process all incoming requests.
1import { http, HttpResponse } from 'msw' 2import { createServer } from '@mswjs/http-middleware' 3 4const httpServer = createServer( 5 http.get('/user', () => { 6 return HttpResponse.json({ firstName: 'John' }) 7 }), 8) 9 10httpServer.listen(9090)
Making a GET http://localhost:9090/user
request returns the following response:
1200 OK 2Content-Type: application/json 3 4{ 5 "firstName": "John" 6}
createMiddleware(...handlers: RequestHandler[])
Creates an Express middleware function that uses the given request handlers to process all incoming requests.
1import { http, HttpResponse } from 'msw' 2import { createMiddleware } from '@mswjs/http-middleware' 3 4const app = express() 5 6app.use( 7 createMiddleware( 8 http.get('/user', () => { 9 return HttpResponse.json({ firstName: 'John' }) 10 }), 11 ), 12) 13 14app.use(9090)
Mentions
- David Idol, original implementation.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
Found 9/30 approved changesets -- score normalized to 3
Reason
2 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 2
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:14: update your workflow using https://app.stepsecurity.io/secureworkflow/mswjs/http-middleware/release.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:20: update your workflow using https://app.stepsecurity.io/secureworkflow/mswjs/http-middleware/release.yml/main?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/release.yml:26: update your workflow using https://app.stepsecurity.io/secureworkflow/mswjs/http-middleware/release.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:16: update your workflow using https://app.stepsecurity.io/secureworkflow/mswjs/http-middleware/test.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:19: update your workflow using https://app.stepsecurity.io/secureworkflow/mswjs/http-middleware/test.yml/main?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/test.yml:25: update your workflow using https://app.stepsecurity.io/secureworkflow/mswjs/http-middleware/test.yml/main?enable=pin
- Info: 0 out of 4 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 2 third-party GitHubAction dependencies pinned
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/release.yml:1
- Warn: no topLevel permission defined: .github/workflows/test.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
license file not detected
Details
- Warn: project does not have a license file
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 13 are checked with a SAST tool
Reason
13 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-qwcr-r2fm-qrc7
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-pxg6-pf52-xh8x
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-rv95-896h-c2vc
- Warn: Project is vulnerable to: GHSA-qw6h-vgh9-j6wx
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-mwcw-c2x4-8c55
- Warn: Project is vulnerable to: GHSA-9wv6-86v2-598j
- Warn: Project is vulnerable to: GHSA-rhx6-c78j-4q9w
- Warn: Project is vulnerable to: GHSA-m6fv-jmcg-4jfg
- Warn: Project is vulnerable to: GHSA-cm22-4g7w-348p
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
Score
2.9
/10
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More