-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathserver.mjs
More file actions
48 lines (37 loc) · 1.18 KB
/
server.mjs
File metadata and controls
48 lines (37 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import * as dotenv from "dotenv";
dotenv.config()
import { load } from "@azure/app-configuration-provider";
const connectionString = process.env.APPCONFIG_CONNECTION_STRING;
const appConfig = await load(connectionString, {
refreshOptions: {
enabled: true,
refreshIntervalInMs: 5_000
}
});
appConfig.onRefresh(() => {
console.log("Configuration has been refreshed.");
});
import express from "express";
const server = express();
const PORT = 3000;
server.use(express.json());
// Use a middleware to achieve request-driven configuration refresh
server.use((req, res, next) => {
// this call s not blocking, the configuration will be updated asynchronously
appConfig.refresh();
next();
});
server.get("/", (req, res) => {
res.send("Please go to /config to get the configuration.");
});
server.get("/config", (req, res) => {
res.json(appConfig.constructConfigurationObject());
});
server.get("/config/:key", (req, res) => {
res.json(appConfig.get(req.params.key) ?? "");
});
server.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});