// Server.js幾個問題
var http = require('http');
var path = require('path');
var fs = require('fs');
http.createServer(function (request, response) {
console.log('request starting...');
var filePath = '.' + request.url;
if (filePath == './')
filePath = './index.html';
path.exists(filePath, function(exists) {
if (exists) {
fs.readFile(filePath, function(error, content) {
if (error) {
response.writeHead(500);
response.end();
}
else {
response.writeHead(200, { 'Content-Type': 'text/html' });
response.end(content, 'utf-8');
}
});
}
else {
response.writeHead(404);
response.end();
}
});
}).listen(8125);
console.log('Server running at http://127.0.0.1:8125/');
// index.html的
<html>
<head>
<title>Rockin' Page</title>
<link type="text/css" rel="stylesheet" href="style.css" />
<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
</head>
<body>
<p>This is a page. For realz, yo.</p>
</body>
<script type="text/javascript">
$(document).ready(function() {
alert('happenin');
});
</script>
</html>
我能夠運行我的靜態頁面,但我有幾個問題下來該線。
- 接下來我該做什麼?我的意思是要開發什麼和學什麼?我很困惑..與我目前的網絡服務器有什麼不同。
- node.js只是我的Apache Web服務器的替代品。
- 誰能解釋清楚我的的NodeJS的主要目的
有時我覺得這個社區很奇怪。你有兩個投票和三個投票來完成一個完全有效的問題。您發佈了代碼並詢問了3個優秀的答案,我在開始使用node.js時也考慮過這些答案。所以,+1和投票保持開放。 – 2012-03-20 13:30:49