Created
August 6, 2017 19:18
-
-
Save davistrent/5ebcbf2742e211b30df0213ad55f3ac0 to your computer and use it in GitHub Desktop.
async config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = new Promise((resolve, reject) => { | |
console.log('config module loaded!'); | |
const foo = {}; | |
getAsyncConfig(foo) | |
.then(config => { | |
resolve(config); | |
}); | |
}); | |
function getAsyncConfig(foo) { | |
const promise = new Promise((resolve, reject) => { | |
setTimeout(() => { | |
foo.bar = function(test) { | |
console.log('foo.bar() executed with ' + test); | |
}; | |
foo.env = 'development'; | |
foo.port = 8080; | |
resolve(foo); | |
}, 2000); | |
}); | |
return promise; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('./config') | |
.then(config => { | |
console.log(config); | |
require('./server')(config); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
function server(settings) { | |
const config = {}; | |
Object.assign(config, settings); | |
const PORT = config.port; | |
const app = express(); | |
app.get('/', (req, res) => { | |
res.send('Hello World!'); | |
}); | |
app.listen(PORT, () => { | |
console.log(`server listening on port ${PORT}`); | |
}); | |
return app; | |
}; | |
module.exports = server; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment