11import express from "express" ;
22import path from "path" ;
3- import { fileURLToPath } from "url" ;
4- import fs from 'node:fs/promises' ;
53
64import { load } from "@azure/app-configuration-provider" ;
75const connectionString = "<your-connection-string>" ; ;
@@ -33,31 +31,34 @@ We recommend using Azure App Configuration as the source of feature flags.
3331const featureProvider = new ConfigurationMapFeatureFlagProvider ( appConfig ) ;
3432const featureManager = new FeatureManager ( featureProvider ) ;
3533
36- const app = express ( ) ;
34+ const server = express ( ) ;
3735const PORT = 3000 ;
3836
39- const __filename = fileURLToPath ( import . meta. url ) ;
40- const __dirname = path . dirname ( __filename ) ;
37+ // Use a middleware to achieve request-driven configuration refresh
38+ server . use ( ( req , res , next ) => {
39+ // this call s not blocking, the configuration will be updated asynchronously
40+ appConfig . refresh ( ) ;
41+ next ( ) ;
42+ } )
4143
42- const pageDir = path . join ( __dirname , "pages" ) ;
4344
44- app . get ( "/" , ( req , res ) => {
45+ server . get ( "/" , ( req , res ) => {
4546 appConfig . refresh ( ) ;
46- res . sendFile ( path . join ( pageDir , "index.html" ) ) ;
47+ res . send ( "Hello World!" ) ;
4748} ) ;
4849
49- app . get ( "/Beta" , async ( req , res ) => {
50+ server . get ( "/Beta" , async ( req , res ) => {
5051 appConfig . refresh ( ) ;
5152 const { userId, groups } = req . query ;
5253
5354 if ( await featureManager . isEnabled ( "Beta" , { userId : userId , groups : groups ? groups . split ( "," ) : [ ] } ) ) {
54- res . sendFile ( path . join ( pageDir , "beta.html" ) ) ;
55+ res . send ( "Welcome to the Beta page!" ) ;
5556 } else {
5657 res . status ( 404 ) . send ( "Page not found" ) ;
5758 }
5859} ) ;
5960
6061// Start the server
61- app . listen ( PORT , ( ) => {
62+ server . listen ( PORT , ( ) => {
6263 console . log ( `Server is running at http://localhost:${ PORT } ` ) ;
6364} ) ;
0 commit comments