FAQ
Hi there,

I am starting out with learning Node.js development, and I have a small
problem when it comes to running my code. I am using OS X Mountain Lion for
my development, and have a very basic Node.js app that I want to run. The
app consists of several files, one is a pure HTML file, one CSS stylesheet
and a server.js file. The contents of server.js are the following:

var http = require('http')

, fs = require('fs')

, path = require('path')

, mime = require('mime')

, cache = {};

function send404(response) {

response.writeHead(404, {'Content-Type': 'text/plain'});

response.write('Error 404: resource not found.');

response.end();

}


function sendFile(response, filePath, fileContents) {

response.writeHead(

'content-type',

mime.lookup(path.basename(filePath))

);

response.end(fileContents);

}


function serveStatic(response, cache, absPath) {

if (cache[absPath]) {

sendFile(response, absPath, cache[absPath]);

} else {

path.exists(absPath, function(exists) {

if (exists) {

fs.readFile(absPath, function(err, data) {

cache[absPath] = data;

sendFile(response, absPath, data);

});

} else {

send404(response);

}

});

}

}


var server = http.createServer(function(request, response) {

var filePath = false;

if (request.url == '/') {

filePath = 'public/index.html';

} else {

filePath = 'public' + request.url;

}

if (!filePath) {

send404(response);

} else {

var absPath = './' + filePath;

serveStatic(response, cache, absPath);

}

});


server.listen(3000, function() {

console.log("Server listening on port 3000.");

});


I guess I don't need to disclose my HTML and CSS file contents. Now, when I
try to run this app, I get the following response from the browser:


HTTP/1.1 content-type text/html
Date: Thu, 15 Nov 2012 15:19:09 GMT
Connection: keep-alive
Transfer-Encoding: chunked




How do I solve this? The index.html page doesn't get rendered and I get the above response instead. Thanks in advance for help.


--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Search Discussions

  • Ben Noordhuis at Nov 15, 2012 at 4:18 pm

    On Thu, Nov 15, 2012 at 4:39 PM, Petar Petrović wrote:
    Hi there,

    I am starting out with learning Node.js development, and I have a small
    problem when it comes to running my code. I am using OS X Mountain Lion for
    my development, and have a very basic Node.js app that I want to run. The
    app consists of several files, one is a pure HTML file, one CSS stylesheet
    and a server.js file. The contents of server.js are the following:

    var http = require('http')

    , fs = require('fs')

    , path = require('path')

    , mime = require('mime')

    , cache = {};

    function send404(response) {

    response.writeHead(404, {'Content-Type': 'text/plain'});

    response.write('Error 404: resource not found.');

    response.end();

    }


    function sendFile(response, filePath, fileContents) {

    response.writeHead(

    'content-type',

    mime.lookup(path.basename(filePath))

    );
    The argument to writeHead() should be a hash.
    response.end(fileContents);

    }


    function serveStatic(response, cache, absPath) {

    if (cache[absPath]) {

    sendFile(response, absPath, cache[absPath]);

    } else {

    path.exists(absPath, function(exists) {

    if (exists) {

    fs.readFile(absPath, function(err, data) {

    cache[absPath] = data;

    sendFile(response, absPath, data);

    });

    } else {

    send404(response);

    }

    });

    }

    }


    var server = http.createServer(function(request, response) {

    var filePath = false;

    if (request.url == '/') {

    filePath = 'public/index.html';

    } else {

    filePath = 'public' + request.url;

    }

    if (!filePath) {

    send404(response);

    } else {

    var absPath = './' + filePath;

    serveStatic(response, cache, absPath);

    }

    });


    server.listen(3000, function() {

    console.log("Server listening on port 3000.");

    });


    I guess I don't need to disclose my HTML and CSS file contents. Now, when I
    try to run this app, I get the following response from the browser:


    HTTP/1.1 content-type text/html
    Date: Thu, 15 Nov 2012 15:19:09 GMT
    Connection: keep-alive
    Transfer-Encoding: chunked




    How do I solve this? The index.html page doesn't get rendered and I get the
    above response instead. Thanks in advance for help.
    --
    Job Board: http://jobs.nodejs.org/
    Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
    You received this message because you are subscribed to the Google
    Groups "nodejs" group.
    To post to this group, send email to nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Ben Noordhuis at Nov 15, 2012 at 4:14 pm

    On Thu, Nov 15, 2012 at 5:10 PM, Ben Noordhuis wrote:
    function sendFile(response, filePath, fileContents) {

    response.writeHead(

    'content-type',

    mime.lookup(path.basename(filePath))

    );
    The argument to writeHead() should be a hash.
    To avoid further confusion, here is how you use it:

    response.writeHead(200, { 'content-type': 'whatever' })

    --
    Job Board: http://jobs.nodejs.org/
    Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
    You received this message because you are subscribed to the Google
    Groups "nodejs" group.
    To post to this group, send email to nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupnodejs @
categoriesnodejs
postedNov 15, '12 at 3:58p
activeNov 15, '12 at 4:18p
posts3
users2
websitenodejs.org
irc#node.js

2 users in discussion

Ben Noordhuis: 2 posts Petar Petrović: 1 post

People

Translate

site design / logo © 2022 Grokbase