-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
62 lines (50 loc) · 2.16 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
var express = require('express');
var app = express(); // create app with express
var mongoose = require('mongoose'); // mongoose for mongodb
var passport = require('passport');
var flash = require('connect-flash');
var port = process.env.PORT || 8080; // set the port
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var morgan = require('morgan'); // log requests to the console
var bodyParser = require('body-parser'); // pull information from HTML POST
var cookieParser = require('cookie-parser');
var session = require('express-session');
var methodOverride = require('method-override'); // simulate DELETE and PUT
var configDB = require('./config/database');
require('./config/passport')(passport); // pass passport for configuration
mongoose.connect(configDB.url);
app.use(express.static(__dirname + '/public')); // set static file location
app.use(morgan('dev')); // log every request to the console
app.use(cookieParser()); // read cookies (needed for auth)
app.use(bodyParser.urlencoded({'extended':'true'}));
app.use(bodyParser.json());
app.use(bodyParser.json({type: 'application/vnd.api+json'}));
app.use(methodOverride());
app.set('view engine', 'ejs'); // set up ejs for templating
// required for passport
app.use(session({secret: 'ilovescotchscotchyscotchscotch'})); // session secret
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session
// routes ==========
require('./app/routes')(app, passport); // load our routes and pass in our fully configured passport
// socketio =============
io.on('connection', function(socket) {
"use strict";
console.log('a user connected');
socket.on('join', function(data) {
console.log(data);
socket.emit('messages', 'Hello from server');
});
socket.on('chat message', function(msg) {
socket.emit('chat message', msg);
});
socket.on('disconnect', function() {
console.log('a user disconnected');
})
});
// launch ==========
// app.listen(port);
server.listen(port);
console.log("App listening on port 8080");