Skip to content

Commit fe6bb86

Browse files
feat(middleware): add methods option (options.methods) (#319)
1 parent ad88fed commit fe6bb86

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ for the Object.
6262

6363
_Note: The `publicPath` property is required, whereas all other options are optional_
6464

65+
### methods
66+
67+
Type: `Array`
68+
Default: `[ 'GET' ]`
69+
70+
This property allows a user to pass the list of HTTP request methods accepted by the server.
71+
6572
### headers
6673

6774
Type: `Object`

lib/middleware.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ module.exports = function wrapper(context) {
2424
}));
2525
}
2626

27-
if (req.method !== 'GET') {
27+
const acceptedMethods = context.options.methods || ['GET'];
28+
if (acceptedMethods.indexOf(req.method) === -1) {
2829
return goNext();
2930
}
3031

test/tests/server.js

+28
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,34 @@ describe('Server', () => {
122122
});
123123
});
124124

125+
describe('accepted methods', () => {
126+
before((done) => {
127+
app = express();
128+
const compiler = webpack(webpackConfig);
129+
instance = middleware(compiler, {
130+
stats: 'errors-only',
131+
methods: ['POST'],
132+
logLevel,
133+
publicPath: '/public/'
134+
});
135+
app.use(instance);
136+
listen = listenShorthand(done);
137+
});
138+
after(close);
139+
140+
it('POST request to bundle file with methods set to [\'POST\']', (done) => {
141+
request(app).post('/public/bundle.js')
142+
.expect('Content-Type', 'application/javascript; charset=UTF-8')
143+
.expect('Content-Length', '3645')
144+
.expect(200, /console\.log\('Hey\.'\)/, done);
145+
});
146+
147+
it('GET request to bundle file with methods set to [\'POST\']', (done) => {
148+
request(app).get('/public/bundle.js')
149+
.expect(404, done);
150+
});
151+
});
152+
125153
describe('no index mode', () => {
126154
before((done) => {
127155
app = express();

0 commit comments

Comments
 (0)