Skip to content

Commit 911a5e5

Browse files
committed
docs: add example
1 parent 4537e73 commit 911a5e5

File tree

4 files changed

+77
-4
lines changed

4 files changed

+77
-4
lines changed

examples/setup-middlewares/README.md

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# setupMiddlewares
2+
3+
Provides the ability to execute a custom function and apply custom middleware(s).
4+
5+
**webpack.config.js**
6+
7+
```js
8+
module.exports = {
9+
// ...
10+
devServer: {
11+
setupMiddlewares: (middlewares, devServer) => {
12+
if (!devServer) {
13+
throw new Error("webpack-dev-server is not defined");
14+
}
15+
16+
const sendResponses = () => {
17+
devServer.app.get("/setup-middleware/some/path", (_, response) => {
18+
response.send("setup-middlewares option GET");
19+
});
20+
};
21+
22+
middlewares.push(sendResponses());
23+
24+
return middlewares;
25+
},
26+
},
27+
};
28+
```
29+
30+
To run this example use the following command:
31+
32+
```console
33+
npx webpack serve --open
34+
```
35+
36+
## What Should Happen
37+
38+
1. The script should open `http://localhost:8080/` in your default browser.
39+
2. You should see the text on the page itself change to read `Success!`.
40+
3. Go to `http://localhost:8080/setup-middleware/some/path`, you should see the text on the page itself change to read `setup-middlewares option GET`.

examples/setup-middlewares/app.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"use strict";
2+
3+
const target = document.querySelector("#target");
4+
5+
target.classList.add("pass");
6+
target.innerHTML = "Success!";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"use strict";
2+
3+
// our setup function adds behind-the-scenes bits to the config that all of our
4+
// examples need
5+
const { setup } = require("../util");
6+
7+
module.exports = setup({
8+
context: __dirname,
9+
entry: "./app.js",
10+
devServer: {
11+
setupMiddlewares: (middlewares, devServer) => {
12+
if (!devServer) {
13+
throw new Error("webpack-dev-server is not defined");
14+
}
15+
16+
const sendResponses = () => {
17+
devServer.app.get("/setup-middleware/some/path", (_, response) => {
18+
response.send("setup-middlewares option GET");
19+
});
20+
};
21+
22+
middlewares.push(sendResponses());
23+
24+
return middlewares;
25+
},
26+
},
27+
});

test/e2e/setup-middlewares.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ describe("setupMiddlewares option", () => {
2323
throw new Error("webpack-dev-server is not defined");
2424
}
2525

26-
const sendResponses = (server) => {
27-
server.app.get("/setup-middleware/some/path", (_, response) => {
26+
const sendResponses = () => {
27+
devServer.app.get("/setup-middleware/some/path", (_, response) => {
2828
response.send("setup-middlewares option GET");
2929
});
3030

31-
server.app.post("/setup-middleware/some/path", (_, response) => {
31+
devServer.app.post("/setup-middleware/some/path", (_, response) => {
3232
response.send("setup-middlewares option POST");
3333
});
3434
};
3535

36-
middlewares.push(sendResponses(devServer));
36+
middlewares.push(sendResponses());
3737

3838
return middlewares;
3939
},

0 commit comments

Comments
 (0)