Skip to content

Instantly share code, notes, and snippets.

@crapier
Last active August 29, 2015 14:10

Revisions

  1. crapier renamed this gist Nov 20, 2014. 1 changed file with 18 additions and 1 deletion.
    19 changes: 18 additions & 1 deletion Node HTTP Server → Node HTTP Server.js
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,17 @@
    // 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
    @@ -98,4 +105,14 @@ function handler(request, response) {
    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);
  2. crapier created this gist Nov 20, 2014.
    101 changes: 101 additions & 0 deletions Node HTTP Server
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,101 @@
    // The node.js HTTP server.
    var app = require('http').createServer(handler);
    // Allows access to local file system.
    var fs = require('fs');
    // Allows for parsing urls
    var url = require('url');

    // 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);
    }
    }