34
loading...
This website collects cookies to deliver better user experience
http-middleware
is extremely straightforward: reuse your request handlers to create an actual HTTP server.curl
your mock definitions, for example, for local debugging;$ npm init -y
$ npm install express msw @mswjs/http-middleware --save-dev
server.js
file where we will declare our server:$ touch server.js
createServer
function to spawn an Express server. Provide it with the request handlers you want to be responsible for producing responses:// server.js
const { rest } = require('msw')
const { createServer } = require('@mswjs/http-middleware')
const httpServer = createServer(
rest.get('/', (req, res, ctx) => {
return res(ctx.text('Hello world'))
})
)
httpServer.listen(9090)
Learn more about writing request handlers with Mock Service Worker. You can reuse the same handlers you write in tests, local development, and debugging.
$ node server.js
GET http://localhost:9090
request. You'll see that the response was resolved based on the request handler you've specified:200 OK
Content-Type: text/plain;
"Hello world"
// existing-server.js
import { rest } from 'msw'
import { createMiddleware } from '@mswjs/http-middleware'
import { app } from './app'
app.use(
createMiddleware(
rest.get('/', (req, res, ctx) => {
return res(ctx.text('Hello world'))
})
)
)
http-middleware
package with your ideas and feedback on GitHub: