Skip to content

Commit a26417b

Browse files
add express example
1 parent d649dd3 commit a26417b

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

examples/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"dependencies": {
3-
"@azure/app-configuration-provider": "latest",
3+
"@azure/app-configuration-provider": "2.0.0-preview.2",
44
"@azure/identity": "^4.1.0",
5-
"dotenv": "^16.3.1"
5+
"dotenv": "^16.3.1",
6+
"express": "^4.21.2"
67
}
78
}

examples/server.mjs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
import * as dotenv from "dotenv";
5+
dotenv.config()
6+
7+
import { load } from "@azure/app-configuration-provider";
8+
const connectionString = process.env.APPCONFIG_CONNECTION_STRING;
9+
const appConfig = await load(connectionString, {
10+
refreshOptions: {
11+
enabled: true
12+
}
13+
});
14+
15+
appConfig.onRefresh(() => {
16+
console.log("Config refreshed.");
17+
});
18+
19+
import express from "express";
20+
21+
const app = express();
22+
const PORT = 3000;
23+
24+
app.use(express.json());
25+
26+
app.get("/", (req, res) => {
27+
appConfig.refresh();
28+
res.send("Please go to /config to get the configuration.");
29+
});
30+
31+
app.get("/config", (req, res) => {
32+
appConfig.refresh();
33+
res.json(appConfig.constructConfigurationObject());
34+
});
35+
36+
app.get("/config/:key", (req, res) => {
37+
appConfig.refresh();
38+
res.json(appConfig.get(req.params.key) ?? "");
39+
});
40+
41+
app.listen(PORT, () => {
42+
console.log(`Server is running on http://localhost:${PORT}`);
43+
});

0 commit comments

Comments
 (0)