节点和路由管理

时间:2013-03-19 08:45:33

标签: node.js

我写了以下代码:

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:我使用node命令作为节点runServer.js

运行上述代码

步骤2:现在我打开浏览器并输入以下网址

http://localhost:8000/

浏览器正确显示index.html的内容。 index.html文件内容包含一些原始文本并链接到另一个文件,即aboutus.html

步骤3:当我点击关于aboutus.html的链接时,浏览器会将网址更改为以下

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

1 个答案:

答案 0 :(得分: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,从那时起,apis已经改变了一点,fs.exists,而不是path.exists。