2013-03-19 114 views
0

我已经写下面的代码:节点和路由管理

var http = require('http'); 
var fs = require('fs'); 

var fileName = 'site/index.html'; 
var encoding = 'utf-8'; 
var dataContent = ""; 

fs.readFile(fileName, encoding, function(error, data) { 
    if(error) throw error; 
    dataContent = data; 
}); 

var requestListener = function(request, response) { 
    response.writeHead(200, { 
     'Content-Length': dataContent.length, 
     'Content-Type': 'text/html', 
     'connection': 'keep-alive', 
     'accept': '*/*' 
    }); 

    response.end(dataContent); 
} 

var server = http.createServer(requestListener, function(connect) { 
    connect.on('end', function() { 
     console.log('Server disconnected...'); 
    }); 
}); 

server.listen(8000, function() { 
    console.log("Server is listening..."); 
}); 

我有2个文件中的网站目录:

1. index.html 
2. aboutus.html 

步骤1:我使用节点命令作为节点RUNSERVER运行上面的代码。 JS

第2步:现在我已经打开浏览器和类型以下网址

http://localhost:8000/ 

浏览器正确显示了index.html的内容。 而index.html文件内容,一些原始的文字和链接,即aboutus.html

第三步另一个文件:当我点击点击链接aboutus.html浏览器更改URL为以下

http://localhost:8000/aboutus.html 

但aboutus.html的内容不显示,而是它显示我的内容index.html

我知道这是因为fileName变量内容'site/index.html'发生。所以浏览器正在渲染index.html内容

我该如何改变这种行为?如果我不使用express.js


现在,我做了下面的代码一些变化:

var http = require('http'); 
var fs = require('fs'); 
var path = require('path'); 

var fileName = "site/index.html"; 
var encoding = 'utf-8'; 
var dataContent = ""; 

function readContentFile(fileName) { 
    console.log(fileName); 
    fs.readFile(fileName, encoding, function(error, data) { 
     if(error) throw error; 
     dataContent = data; 
    }); 
} 

readContentFile(fileName); 

var requestListener = function(request, response) { 

    filePath = request.url;  
    if(filePath!='/') { 
     fileName = 'site' + filePath; 
     readContentFile(fileName); 
    } 

    response.writeHead(200, { 
     'Content-Length': dataContent.length, 
     'Content-Type': 'text/html', 
     'connection': 'keep-alive', 
     'accept': '*/*' 
    }); 

    response.end(dataContent); 
} 

var server = http.createServer(requestListener, function(connect) { 



    connect.on('end', function() { 
     console.log('Server disconnected...'); 
    }); 
}); 

server.listen(8000, function() { 
    console.log("Server is listening..."); 
}); 

不过其没有工作,是什么东西错在我的代码。 或者我应该去express.js

+0

任何特殊原因不使用Express? – robertklep 2013-03-19 09:01:53

+0

我是新node.js 所以,我正在考虑不使用express.js – 2013-03-19 09:05:34

+1

我刚开始使用时,我开始使用的第一个框架之一是快递:) – robertklep 2013-03-19 09:09:07

回答

1

我创建了一个静态的服务器

看看@http://runnable.com/UUgnuT2wDj1UAGSe

var http = require('http'); 
var fs = require('fs'); 
var path = require('path'); 

http.createServer(function (req, res) { 
    if (req.url === '/') { 
    req.url = '/index.html'; 
    } 
    var file = path.join(__dirname,req.url); 
    path.exists(file, function (exists) { 
    if (exists) { 
     fs.stat(file, function (err, stats) { 
     if(err) { 
      throw err; 
     } 
     if (stats.isFile()) { 
      res.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'}); 
      fs.createReadStream(file).pipe(res); 
     } else { 
      res.writeHead(404); 
      res.end('not found'); 
     } 
     }); 
    } else { 
     res.writeHead(404); 
     res.end('not found'); 
    } 
    }); 
}).listen(8000); 

console.log('Server running on port ' + 8000); 

笔记的例子:这是使用节点0.6.x的API已经改变从那时起,fs.exists,而不是path.exists。

+1

这将允许任何人从您的服务器检索*任何*文件... – robertklep 2013-03-19 09:25:25

+0

确实,这是一个使用路径和fs api,回调和管道的示例。但它远不是生产代码,只是一个POC。 – generalhenry 2013-03-19 09:27:39

+0

d:\的NodeJS>节点RunServer.js 服务器运行在端口未定义 ^ C d:\的NodeJS> – 2013-03-19 09:30:27