-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
64 lines (49 loc) · 1.63 KB
/
server.js
File metadata and controls
64 lines (49 loc) · 1.63 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
56
57
58
59
60
61
62
63
64
//SETUP
var express=require('express')
var app=express()
// express initializes app to be a function handler
var http = require('http').Server(app);
// app is supplied to http server
var io = require('socket.io')(http);
// initializes a new instance by passing in http object
var morgan = require('morgan')
//log requests to the console
var bodyParser = require('body-parser')
//pull information from the html POST
var jwt = require('jwt-simple')
//
var config = require('./configure')
//
//parse application/json
app.use(morgan('dev'))
//log every request to the console
app.use(bodyParser.json())
app.use(require('./auth'))
// app.use(function (req, res, next) {
// //need to decode JWT sessions for logged in users
// if (req.headers['x-auth']) {
// req.auth = jwt.decode(req.header('x-auth'), config.secret)
// }
// next() //call the next middleware function
// })
//ROUTES
//delivering static content (index.html and js files...)
app.use('/',require('./controllers/static'))
//posting new messages and displaying messages
app.use('/api/messages',require('./controllers/api/messages'))
//for logging in as an existing user
app.use('/api/sessions',require('./controllers/api/sessions'))
//for registering new users
app.use('/api/users',require('./controllers/api/users'))
//SOCKETS
//listen for connection event for incoming sockets
io.on('connection',function(socket){
socket.on('chat message', function(msg){
// emit the event from the server to the rest of the users
io.emit('chat message', msg);
});
})
var port=process.env.PORT || 8080
http.listen(port,function(){
console.log('Magic happens on port '+port)
})