-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
47 lines (37 loc) · 1.35 KB
/
app.js
File metadata and controls
47 lines (37 loc) · 1.35 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
"use strict"
const express = require('express')
const bodyParser = require('body-parser')
const config = require('./config/config')
const app = module.exports = express()
const server = require('http').createServer(app)
const io = require('socket.io')(server)
io.on('connection', function (socket) {
socket.emit('getMyUsername', generateUsername())
socket.on('send', function (data) {
socket.emit('updateMsg', data)
socket.broadcast.emit('updateMsg', data)
})
})
/**
* Set headers to allow cross-origin
*/
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', process.env.NODE_ENV === 'production' ? config.prod.allowOrigin : config.dev.allowOrigin)
res.header('Access-Control-Allow-Credentials', true)
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
next()
})
server.listen(process.env.PORT || process.env.NODE_ENV === 'production' ? config.prod.port : config.dev.port)
/**
* Helper function to generate
* @return {string} generated username
*/
function generateUsername(length)
{
length = length === undefined ? 7 : length
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < length; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}