// 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