Last active
October 9, 2018 21:02
-
-
Save nafeu/f284ad459dc09a8ee3e0aabd7012f4fb to your computer and use it in GitHub Desktop.
Simple Express.js static file/folder configuration with Express API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express') | |
, app = express() | |
, http = require('http') | |
, server = require('http').Server(app) | |
, bodyParser = require('body-parser') | |
, io = require('socket.io')(server); | |
// --------------------------------------------------------------------------- | |
// Configuration | |
// --------------------------------------------------------------------------- | |
// Server | |
server.listen(process.env.PORT || 8000, function(){ | |
console.log('[ server.js ] Listening on port ' + server.address().port); | |
}); | |
// Socket.io configs | |
io.set('heartbeat timeout', 4000); | |
io.set('heartbeat interval', 2000); | |
// Express server configs | |
app.use(bodyParser.urlencoded({ | |
extended: true | |
})); | |
app.use(bodyParser.json()); | |
app.use(express.static(__dirname + '/public')); | |
// --------------------------------------------------------------------------- | |
// Socket Event Listeners | |
// --------------------------------------------------------------------------- | |
io.on('connection', function(socket){ | |
console.log(socket.id + " connected..."); | |
socket.on('disconnect', function(){ | |
console.log(socket.id + " disconnected..."); | |
}); | |
}); | |
// --------------------------------------------------------------------------- | |
// Express API | |
// --------------------------------------------------------------------------- | |
app.get('/api/test', function(req, res){ | |
res.status(200).send('OK'); | |
}); | |
// --------------------------------------------------------------------------- | |
// Application Logic | |
// --------------------------------------------------------------------------- | |
// ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment