-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
40 lines (32 loc) · 1 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
var express = require('express')
var app = express()
var debug = require('debug')('pubsub-server')
var fs = require('fs')
var server
var options = require('./options')
// debugging
if (!options.debug || options.output == 'stdout' || options.output == 'console') {
debug = console.log.bind(console)
}
//SSL cert and key
var ssl_options = {
cert: options.cert ? fs.readFileSync(options.cert) : null,
key: options.key ? fs.readFileSync(options.key) : null,
}
// create a https or http server
if (options.cert && options.key) {
server = require('https').createServer(ssl_options, app)
} else {
server = require('http').createServer(app)
}
var io = require('socket.io')(server)
var bodyParser = require('body-parser')
app.use( bodyParser.json() ) // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}))
// Routing
app.use(express.static(__dirname + '/public'))
module.exports = server
module.exports.app = app
module.exports.io = io