Closed dsumac closed 3 years ago
No problem to do that, I'm using it and it works fine:
import express from 'express';
import { stubServer } from '@pmu-tech/stub-server';
// ...
const app = express();
app.use(express.json());
stubServer(configPath, app);
// ...
or
// webpack.config.ts
import express from 'express';
import { stubServer } from '@pmu-tech/stub-server';
// ...
devServer: {
before: app => {
app.use(express.json());
const configPath = path.resolve(__dirname, 'stubs', 'config');
stubServer(configPath, app);
}
},
// ...
Should it be enabled by stubServer? Not sure.
Express does not parse JSON by default, why?
webpack-dev-server doesn't parse JSON by default: https://github.com/webpack/webpack-dev-server/blob/v3.10.3/lib/Server.js#L164. But they test that it works with it: https://github.com/webpack/webpack-dev-server/blob/v3.10.3/test/server/proxy-option.test.js#L327 (btw this is the old way to do it).
koa does not parse JSON by default
Fastify however parses JSON by default
Yes, finally I think it's not stub-server's responsability to do that
Purpose
In some of cases we would like parse the request body to make some logic. For example, if body contains some attribute/value I would like return stub A and if it contains another attribute/value I would like return B.
Actual behavior
With this configuration:
We have this result:
Desired behaviour
We should consider to use body-parser: