Instantly share code, notes, and snippets.

Embed
What would you like to do?
Quick Express Seed
const
express = require('express'),
logger = require('morgan'),
path = require('path'),
bodyParser = require('body-parser'),
serveStatic = express.static,
MongoClient = require('mongodb').MongoClient,
io = require('socket.io'),
http = require('http');
let app = express(),
server = http.createServer(app);
// ## View Engine Setup
// app.set('views', path.resolve('build'));
// app.set('view engine', 'html');
// app.engine('html', hbs.express4());
app.set('port', process.env.PORT || 3001);
// setup some middlewares
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(serveStatic( path.resolve('build') ));
// First connect to mongodb and then start the server
MongoClient.connect('mongodb://localhost:27017/moviedb').then(db => {
app.set('mongodb', db); // store in express local.
// start the server
server.listen(app.get('port'), () => {
console.log('Listening on: localhost:' + app.get('port'));
});
}).catch(e => console.error('Failed to connect to mongodb. Make sure you start mongod instance.', e));
// connect to socketio.
io(server).on('connection', (socket) => {
console.log('Socket.IO Connected. ID: ', socket.id);
console.log(app.get('mongodb'));
let db = app.get('mongodb');
if(db) {
// db calls.
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment