Skip to content

Instantly share code, notes, and snippets.

@JosephLivengood
Created February 12, 2017 19:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JosephLivengood/3e4b7750f6cd42feaa2768458d682136 to your computer and use it in GitHub Desktop.
Save JosephLivengood/3e4b7750f6cd42feaa2768458d682136 to your computer and use it in GitHub Desktop.
FCC Advanced Node and Express Socket.IO Checkpoint 2 (Server)
'use strict';
const express = require('express');
const session = require('express-session');
const bodyParser = require('body-parser');
const fccTesting = require('./freeCodeCamp/fcctesting.js');
const auth = require('./app/auth.js');
const routes = require('./app/routes.js');
const mongo = require('mongodb').MongoClient;
const cookieParser= require('cookie-parser')
const passport = require('passport');
const sessionStore= new session.MemoryStore();
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const passportSocketIo = require("passport.socketio");
fccTesting(app); //For FCC testing purposes
app.use('/public', express.static(process.cwd() + '/public'));
app.use(cookieParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'pug')
app.use(session({
secret: process.env.SESSION_SECRET,
resave: true,
saveUninitialized: true,
key: 'express.sid',
store: sessionStore,
}));
mongo.connect(process.env.DATABASE, (err, db) => {
if(err) console.log('Database error: ' + err);
auth(app, db);
routes(app, db);
http.listen(process.env.PORT || 3000);
//start socket.io code
io.use(passportSocketIo.authorize({
cookieParser: cookieParser,
key: 'express.sid',
secret: process.env.SESSION_SECRET,
store: sessionStore
}));
var currentUsers = 0;
io.on('connection', socket => {
++currentUsers;
console.log('user ' + socket.request.user.name + ' connected');
io.emit('user', {name: socket.request.user.name, currentUsers, connected: true});
socket.on('chat message', (message) => {
io.emit('chat message', {name: socket.request.user.name, message});
});
socket.on('disconnect', () => {
--currentUsers;
io.emit('user', {name: socket.request.user.name, currentUsers, connected: true});
});
});
//end socket.io code
});
@chaudha4
Copy link

If you are having problems in getting the tests to pass, make sure that its is not due to CORs validation. Look at the allowedOrigins in fcctesting.js and add the domain - In my case, I had to add "https://www.freecodecamp.org" and then the tests started to pass.

@MikeyWilson2907
Copy link

i think connected in line 68 should be "false"

@Azarakhsh
Copy link

i think connected in line 68 should be "false"

So do I.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment