metamaps--metamaps/server.js

46 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-02-15 19:16:54 +00:00
const path = require('path')
const express = require('express')
2018-03-06 04:27:38 +00:00
const cookieParser = require('cookie-parser')
2018-02-15 19:16:54 +00:00
const webpack = require('webpack')
const socketio = require('socket.io')
const { createServer } = require('http')
2018-02-15 19:16:54 +00:00
const webpackDevMiddleware = require('webpack-dev-middleware')
2018-03-04 02:49:11 +00:00
const apiProxyMiddleware = require('./apiProxyMiddleware')
const realtime = require('./realtime')
2018-03-04 02:49:11 +00:00
const port = process.env.PORT || 3000
2018-02-15 19:16:54 +00:00
const app = express()
const server = createServer(app)
const io = socketio(server)
realtime(io) // sets up the socketio event listeners
2018-02-15 19:16:54 +00:00
2018-03-06 04:27:38 +00:00
app.use(cookieParser())
// serve the whole public folder as static files
2018-02-15 19:16:54 +00:00
app.use(express.static(path.join(__dirname, 'public')))
const config = require('./webpack.config.js')
const compiler = webpack(config)
2018-02-15 19:16:54 +00:00
// 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
2018-03-04 02:49:11 +00:00
}))
// pass XMLHttpRequests
// through to the API
// anything which is the UI wanting to make requests to the API
2018-03-04 02:49:11 +00:00
app.use(apiProxyMiddleware)
2018-02-15 19:16:54 +00:00
// for any normal route
2018-02-15 19:16:54 +00:00
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'))
2018-02-15 19:16:54 +00:00
})
2018-03-04 02:49:11 +00:00
// Serve the files on set port or port 3000.
server.listen(port, function () {
2018-03-04 02:49:11 +00:00
console.log('Metamaps listening on port ' + port + '\n')
2018-02-15 19:16:54 +00:00
});