mswjs / http-middleware

Spawn an HTTP server from your request handlers or apply them to an existing server using a middleware.

Home Page:https://npm.im/@mswjs/http-middleware

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CORS support

xjamundx opened this issue · comments

I was going to ask for CORS support in the createServer method, but then I did this instead using createMiddleware:

import { createMiddleware } from '@mswjs/http-middleware';
import express from 'express';
import cors from 'cors';
import { handlers } from './handlers';

const app = express();

app.use(cors());
app.use(express.json());
app.use(createMiddleware(...handlers));

app.listen(8082);

Opening purely so if someone searches for CORS they can see how to add it.

Or maybe you can put it in the docs.

Hi, @xjamundx. Thanks for leaving this here for posterity!

Yes, in the case of more specific behaviors, such as adding a CORS middleware, it's expected to use this library via the createMiddleware() function, attaching it to your own server. I believe that to be implied knowledge but I may add a mention to the docs regarding this.