{"size": 2460, "ext": "js", "max_stars_repo_path": "server/server.js", "max_stars_repo_name": "Thieurom/fcc-lookstocks", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "server/server.js", "max_issues_repo_name": "Thieurom/fcc-lookstocks", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "server/server.js", "max_forks_repo_name": "Thieurom/fcc-lookstocks", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 26.170212766, "max_line_length": 70, "alphanum_fraction": 0.6451219512}
const express = require('express');
const mongoose = require('mongoose');
const bodyPaser = require('body-parser');
const session = require('express-session');
const csrf = require('csurf')
const favicon = require('serve-favicon');
const path = require('path');

require('dotenv').config({ path: __dirname + '/config/.env' });
const PORT = parseInt(process.env.PORT, 10);
const DATABASE = process.env.DATABASE;
const SESSION_SECRET = process.env.SESSION_SECRET;

const app = express();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
const MongoStore = require('connect-mongo')(session);
let db;


mongoose.connect(DATABASE);

db = mongoose.connection;
db.on('err', console.error.bind(console, 'Connection error!'));
db.once('open', () => {
    console.log('Connected to database server.');
});


// Set appPath directory according to environment
if (app.get('env') === 'development') {
    app.set('appPath', path.join(__dirname, '../client/app'));
} else {
    app.set('appPath', path.join(__dirname, '../client/dist'));
}

app.use(favicon(path.join(__dirname, 'favicon.ico')));

app.use(express.static(app.get('appPath')));
app.use(express.static(path.join(app.get('appPath'), '../.tmp')));

app.use(session({
    secret: SESSION_SECRET,
    resave: true,
    saveUninitialized: true,
    store: new MongoStore({ mongooseConnection: mongoose.connection })
}));
app.use(bodyPaser.json());
app.use(csrf());


// Routing
app.use((req, res, next) => {
    res.cookie('x-csrf-token', req.csrfToken());
    next();
});

app.get('/', (req, res) => {
    res.sendFile(path.join(app.get('appPath'), '/home.html'));
});

app.use('/api/stocks', require('./routes/api'));

// Catch 404 error
app.use((req, res, next) => {
    let err = new Error('Not found');
    err.status = 404;
    return next(err);
});

// Error handler
app.use((err, req, res, next) => {
    let status = err.status || 500;
    let message = app.get('env') === 'development' ? err.message : {};

    res.status(status).send({ error: message });
});


io.on('connection', client => {
    console.log('A client connected.');
    client.on('stock addition', symbol => {
        client.broadcast.emit('stock addition', symbol);
    });

    client.on('stock removal', symbol => {
        client.broadcast.emit('stock removal', symbol);
    });
});

// Establish server
server.listen(PORT, () => {
    console.log(`Server is listening on port ${PORT}!`);
});