Skip to content

Commit

Permalink
docs: add example
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Dec 3, 2021
1 parent 4537e73 commit 911a5e5
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 deletions.
40 changes: 40 additions & 0 deletions examples/setup-middlewares/README.md
@@ -0,0 +1,40 @@
# setupMiddlewares

Provides the ability to execute a custom function and apply custom middleware(s).

**webpack.config.js**

```js
module.exports = {
// ...
devServer: {
setupMiddlewares: (middlewares, devServer) => {
if (!devServer) {
throw new Error("webpack-dev-server is not defined");
}

const sendResponses = () => {
devServer.app.get("/setup-middleware/some/path", (_, response) => {
response.send("setup-middlewares option GET");
});
};

middlewares.push(sendResponses());

return middlewares;
},
},
};
```

To run this example use the following command:

```console
npx webpack serve --open
```

## What Should Happen

1. The script should open `http://localhost:8080/` in your default browser.
2. You should see the text on the page itself change to read `Success!`.
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`.
6 changes: 6 additions & 0 deletions examples/setup-middlewares/app.js
@@ -0,0 +1,6 @@
"use strict";

const target = document.querySelector("#target");

target.classList.add("pass");
target.innerHTML = "Success!";
27 changes: 27 additions & 0 deletions examples/setup-middlewares/webpack.config.js
@@ -0,0 +1,27 @@
"use strict";

// our setup function adds behind-the-scenes bits to the config that all of our
// examples need
const { setup } = require("../util");

module.exports = setup({
context: __dirname,
entry: "./app.js",
devServer: {
setupMiddlewares: (middlewares, devServer) => {
if (!devServer) {
throw new Error("webpack-dev-server is not defined");
}

const sendResponses = () => {
devServer.app.get("/setup-middleware/some/path", (_, response) => {
response.send("setup-middlewares option GET");
});
};

middlewares.push(sendResponses());

return middlewares;
},
},
});
8 changes: 4 additions & 4 deletions test/e2e/setup-middlewares.test.js
Expand Up @@ -23,17 +23,17 @@ describe("setupMiddlewares option", () => {
throw new Error("webpack-dev-server is not defined");
}

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

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

middlewares.push(sendResponses(devServer));
middlewares.push(sendResponses());

return middlewares;
},
Expand Down

0 comments on commit 911a5e5

Please sign in to comment.