-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
81 lines (70 loc) · 2.73 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
* Friends with Friends: Real-time Multi-player Card Game
* Copyright (C) 2015 Waitaya Krongapiradee
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// www.sitepoint.com/preparing-ecmascript-6-let-const
let http = require('http')
let path = require('path')
let express = require('express')
let babelMiddleware = require("babel-connect")
let sassMiddleware = require('express-dart-sass')
let app = express()
let server = http.createServer(app)
// https://stackoverflow.com/a/25896899
let io = require('socket.io')(server)
global.io = io;
global.development = process.env.NODE_ENV !== 'production'
//process.env.PORT
//require config.json
//options ||= config.json ||=default
//TODO: priority: which comes first, config file or env variable?
// Security Stuff
// http://expressjs.com/advanced/best-practice-security.html
app.disable('x-powered-by')
//TODO: add other middlewares such as compression() and
// https://stackoverflow.com/q/16978256
// Middlewares
app.use('/assets/css', sassMiddleware({
/* Options */
src: path.join(__dirname, 'assets', 'css'),
dest: path.join(__dirname, 'tmp', 'sass'),
//dest: path.join(__dirname, 'public', 'assets', 'css'),
debug: global.development, //TODO: if production write to log file instead
outputStyle: global.development ? 'expanded' : 'compressed'
}))
//TODO: this is just a quick fix; open a pull request for middleware to send output in request everytime like babel
app.use('/assets/css', express.static(path.join(__dirname, 'tmp', 'sass')))
//TODO: what is ast
//no sourcemap please :)
app.use('/assets/js', babelMiddleware({
src: path.join(__dirname, 'assets', 'js'),
dest: path.join(__dirname, 'tmp', 'babel'),
options: {
compact: !global.development,
comments: global.development
}
}))
// the priority of middlewares does matter
app.use(express.static(path.join(__dirname, 'public')))
app.use('/assets/vendor', express.static(path.join(__dirname, 'bower_components')))
// https://stackoverflow.com/a/18283508
require('./lib/app.js')
// listen to me pls.
let port = process.env.PORT || 3000
// TODO: https server
server.listen(port, () => {
console.log(`Server listening at *:${port}`)
})