On Thu, Nov 15, 2012 at 4:39 PM, Petar Petrović
<[email protected]> 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
>
> 0
>
>
> 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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to