Consider the following code snippet below
(async () => {
const app = express();
let test = "test string";
app.get('/numbers', (req, res) => {
res.send(test);
}
app.listen( 8000, () => {
console.log( `server running http://localhost:8000`);
});
)();
If I run the above code snippet, the server starts listening on localhost:8000.
How is that working?
How am I able to access test
variable in my get
method? Wouldn't the test
variable be destroyed once the topmost function has finished executing?