Skip to content

Instantly share code, notes, and snippets.

@crapier
Last active August 29, 2015 14:10
Show Gist options
  • Save crapier/ff96e4fcfafafd4ea7f6 to your computer and use it in GitHub Desktop.
Save crapier/ff96e4fcfafafd4ea7f6 to your computer and use it in GitHub Desktop.
// The node.js HTTP server.
var app = require('http').createServer(handler);
// The socket.io WebSocket server, running with the node.js server.
var io = require('socket.io').listen(app);
// Allows access to local file system.
var fs = require('fs');
// Allows for parsing urls
var url = require('url');
var listen_port = 80;
app.listen(listen_port);
console.log('Server is listening on port: ' + listen_port);
// Handles HTTP requests.
function handler(request, response) {
// Parse the url to get the path
var path = url.parse(request.url).pathname;
switch (path) {
// Return the index page for the client
case '/':
fs.readFile(__dirname + '/client/index.html',
function(err, content) {
if (err) {
// The client requested a file we couldn't find
response.writeHead(404);
return response.end('Could not find file' + path);
}
response.writeHead(200, {'Content-Type': 'text/html'});
response.end(content);
});
break;
default:
// Handle html requests
if (/\.(html)$/.test(path)) {
fs.readFile(__dirname + path,
function(err, content) {
if (err) {
// The client requested a file we couldn't find
response.writeHead(404);
return response.end('Could not find file ' + path);
}
response.writeHead(200, {'Content-Type': 'text/html'});
response.end(content);
});
break;
}
// Handle css requests
if (/\.(css)$/.test(path)) {
fs.readFile(__dirname + path,
function(err, content) {
if (err) {
// The client requested a file we couldn't find
response.writeHead(404);
return response.end('Could not find file ' + path);
}
response.writeHead(200, {'Content-Type': 'text/css'});
response.end(content);
});
break;
}
// Handle javascript requests
if (/\.(js)$/.test(path)) {
fs.readFile(__dirname + path,
function(err, content) {
if (err) {
// The client requested a file we couldn't find
response.writeHead(404);
return response.end('Could not find file ' + path);
}
response.writeHead(200, {'Content-Type': 'application/x-javascript'});
response.end(content);
});
break;
}
// Handle image requests (only png support added at the momment)
if (/\.(png)$/.test(path)) {
fs.readFile(__dirname + path,
function(err, content) {
if (err) {
// The client requested a file we couldn't find
response.writeHead(404);
return response.end('Could not find file ' + path);
}
response.writeHead(200, {'Content-Type': 'Image'});
response.end(content);
});
break;
}
// Handle sound request (only mp3 suport added at the momment)
if (/\.(mp3)$/.test(path)) {
fs.readFile(__dirname + path,
function(err, content) {
if (err) {
// The client requested a file we couldn't find
response.writeHead(404);
return response.end('Could not find file ' + path);
}
response.writeHead(200, {'Content-Type': 'Sound'});
response.end(content);
});
break;
}
// The client requested a file type we don't handle
response.writeHead(501);
return response.end('Can not handle ' + path);
}
}
var disconnect_handler = function() {
}
var connection_handler = function(client) {
client.on('disconnect', disconnect_handler);
}
io.sockets.on('connection', connection_handler);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment