Created
August 8, 2018 21:26
-
-
Save pbabbott/cfb9c766f5a9d3f741aa23b12c28b3ea to your computer and use it in GitHub Desktop.
Example node server
This file contains hidden or 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
import express from 'express'; | |
import dotenv from 'dotenv'; | |
import path from 'path'; | |
const getConfigValue = (name, defaultValue) => { | |
let result = process.env[name]; | |
return result ? result : defaultValue; | |
} | |
// Read configuration | |
dotenv.config({ silent: true }); | |
let config = { | |
port: getConfigValue('PORT', 3000) | |
} | |
// Express server | |
const server = express(); | |
const indexHtml = path.resolve(__dirname, 'index.html'); | |
server.get('/*', (req, res) => { | |
res.sendFile(indexHtml); | |
}); | |
server.listen(config.port, () => { | |
console.log('Express server successfully started'); | |
}); | |
export default server; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment