metamaps--metamaps/server.js
2018-03-11 16:24:57 -04:00

49 lines
No EOL
1.5 KiB
JavaScript

require('dotenv').config()
const path = require('path')
const express = require('express')
const cookieParser = require('cookie-parser')
const webpack = require('webpack')
const socketio = require('socket.io')
const { createServer } = require('http')
const webpackDevMiddleware = require('webpack-dev-middleware')
const apiProxyMiddleware = require('./apiProxyMiddleware')
const realtime = require('./realtime')
const port = process.env.PORT || 3000
const app = express()
const server = createServer(app)
const io = socketio(server)
realtime(io) // sets up the socketio event listeners
app.use(cookieParser())
// serve the whole public folder as static files
app.use(express.static(path.join(__dirname, 'public')))
if (process.env.NODE_ENV === 'development') {
const config = require('./webpack.config.js')
const compiler = webpack(config)
// Tell express to use the webpack-dev-middleware and use the webpack.config.js
// configuration file as a base.
app.use(webpackDevMiddleware(compiler, {
publicPath: config.output.publicPath
}))
}
// pass XMLHttpRequests
// through to the API
// anything which is the UI wanting to make requests to the API
app.use(apiProxyMiddleware)
// for any normal route
app.get('*', function (req, res) {
// respond with the same html file
// since the whole UI technically boots
// from the React app and the javascript
res.sendFile(path.join(__dirname, 'public/index.html'))
})
// Start up the server
server.listen(port, function () {
console.log('Metamaps listening on port ' + port + '\n')
});