Skip to content

Commit 119a739

Browse files
use middleware
1 parent eac3079 commit 119a739

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

examples/server.mjs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import { load } from "@azure/app-configuration-provider";
88
const connectionString = process.env.APPCONFIG_CONNECTION_STRING;
99
const appConfig = await load(connectionString, {
1010
refreshOptions: {
11-
enabled: true
11+
enabled: true,
12+
refreshIntervalInMs: 5_000
1213
}
1314
});
1415

@@ -18,26 +19,30 @@ appConfig.onRefresh(() => {
1819

1920
import express from "express";
2021

21-
const app = express();
22+
const server = express();
2223
const PORT = 3000;
2324

24-
app.use(express.json());
25+
server.use(express.json());
2526

26-
app.get("/", (req, res) => {
27+
// Use a middleware to achieve request-driven configuration refresh
28+
server.use((req, res, next) => {
29+
// this call s not blocking, the configuration will be updated asynchronously
2730
appConfig.refresh();
31+
next();
32+
});
33+
34+
server.get("/", (req, res) => {
2835
res.send("Please go to /config to get the configuration.");
2936
});
3037

31-
app.get("/config", (req, res) => {
32-
appConfig.refresh();
38+
server.get("/config", (req, res) => {
3339
res.json(appConfig.constructConfigurationObject());
3440
});
3541

36-
app.get("/config/:key", (req, res) => {
37-
appConfig.refresh();
42+
server.get("/config/:key", (req, res) => {
3843
res.json(appConfig.get(req.params.key) ?? "");
3944
});
4045

41-
app.listen(PORT, () => {
46+
server.listen(PORT, () => {
4247
console.log(`Server is running on http://localhost:${PORT}`);
4348
});

0 commit comments

Comments
 (0)