-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
55 lines (47 loc) · 1.73 KB
/
index.js
File metadata and controls
55 lines (47 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// TODO: Server dist build.
// TODO: Discuss if we should proceed with babel-node or normal node
// TODO: SSR https://vuejs.org/v2/guide/ssr.html
// TODO: Figure out how to make the server startup faster (if possible).
import { resolve } from 'path'
import express from 'express'
import bodyParser from 'body-parser'
import mongoose from 'mongoose'
import config from './config'
const app = express()
const port = process.env.PORT || 3000
// Config
app.use(bodyParser.json())
// Database
mongoose.connect('mongodb://localhost/spa')
// Models
// TODO: Auto load
require('./models/user.js')
// API
// TODO: Auto load
// TODO: Discuss if the user api url is needed.
// The user object is returned on each authentication call.
// app.use(require('./api/user.js'))
app.use('/api', require('./api/auth.js'))
// Development Server
// Note: We are using amd require for dynamic loading.
if (process.env.NODE_ENV !== 'production') {
const webpack = require('webpack')
const webpackDevMiddleware = require('webpack-dev-middleware')
const webpackHotMiddleware = require('webpack-hot-middleware')
const webpackConfig = require('../webpack.config.babel.js').default
const compiler = webpack(webpackConfig)
const middleware = webpackDevMiddleware(compiler, webpackConfig.devServer)
app.use(middleware)
app.use(webpackHotMiddleware(compiler))
app.get('*', (req, res) => {
res.write(middleware.fileSystem.readFileSync(resolve(__dirname, '../client_dist/index.html')))
res.end()
})
} else {
app.use(express.static('client_dist'))
app.get('*', (req, res) => {
res.sendFile(resolve(__dirname, '../client_dist/index.html'))
})
}
// Start the server
app.listen(port, () => console.log(`Server up and running at http://localhost:${port}/`))