I am running the latest Node.js version (0.8.14 as of this writing) on OS X Mountain Lion (10.8.2). I am writing a simple chat application, and I have one server.js file, one index.html file and one style.css file.
The contents of the server.js file 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 think it's no use to post the content of index.html and style.css page because they consist of standard HTML and CSS stuff. When I run the app with
node server.js
and visit the app at
localhost:3000
I get the following response in the browser (also, it takes about 2 - 3 minutes to actually load the page):
HTTP/1.1 content-type text/html
Date: Thu, 15 Nov 2012 20:08:16 GMT
Connection: keep-alive
Transfer-Encoding: chunked
0
instead of rendering an index.html web page. Where am I going wrong?
Update: I've restarted Aptana Studio, and now when I run
node server.js
I get the following output:
throw err;
^
Error: Cannot find module '/Users/petarpetrovic/Documents/Aptana Studio 3 Worksp
ace/chatroom/sever.js'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.runMain (module.js:492:10)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
Update 2: When I run
npm install
And then run server.js file, I get the following output:
HTTP/1.1 content-type text/html
Date: Thu, 15 Nov 2012 22:16:21 GMT
Connection: keep-alive
Transfer-Encoding: chunked
33c
<!DOCTYPE html>
<html lang='en'>
<head>
<title>Chat</title>
<link rel='stylesheet' href='/stylesheets/style.css'></link>
</head>
<body>
<div id='content'>
<div id='room'></div>
<div id='room-list'></div>
<div id='messages'></div>
<form id='send-form'>
<input id='send-message'>
<input id='send-button' type='button' value='Send' />
<div id='help'>
Chat commands:
<ul>
<li>Change nickname: <code>/nick [username] </code></li>
<li>Join/create room: <code>/join [room name]</code></li>
</ul>
</div>
</form>
</div>
<script src='/socket.io/socket.io.js'></script>
<script src='http://code.jquery.com/jquery-1.8.0.min.js'></script>
<script src='/javascripts/chat.js'></script>
<script src='/javascripts/chat_ui.js'></script>
</body>
</html>
0