-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
53 lines (44 loc) · 1.46 KB
/
server.js
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
require('dotenv').config()
const express = require('express')
const cors = require('cors')
const glob = require('glob')
const path = require('path')
const config = require('./server/config')
const webpack = require('webpack')
const webpackConfig = require('./webpack.config.js')
const app = express()
const webpackMiddleware = require('webpack-dev-middleware')
const webpackHotMiddleware = require('webpack-hot-middleware')
const compiler = webpack(webpackConfig)
app.use(require("webpack-dev-middleware")(compiler, {
noInfo: true, publicPath: webpackConfig.output.publicPath
}));
app.use(require("webpack-hot-middleware")(compiler, {
log: console.log, path: '/__webpack_hmr', heartbeat: 10 * 1000
}));
app.use(express.static( path.join( __dirname, 'public')))
app.use(cors())
app.set('view engine','ejs')
app.set('views', __dirname + '/server/views')
app.use((req, res, next)=>{
if(req.originalUrl!==config.apiPrefix+'/' && req.originalUrl.includes(config.apiPrefix)){
const token = req.get('Authorization')
if (token) {
req.token = token
next()
} else {
res.status(403).send({
error: 'Please provide an Authorization header to identify yourself (can be whatever you want)'
})
}
}else{
next()
}
})
glob.sync('./server/routes/*.js').forEach(file=>{
const route = require(path.resolve(file))
app.use(route)
})
app.listen(config.port, () => {
console.log('Server listening on port %s, Ctrl+C to stop', config.port)
})