0

我是节点 js 的新手。我正在关注链接。但这总是执行response.writeHead(404);所以我无法查看 index.html http://thecodinghumanist.com/blog/archives/2011/5/6/serving-static-files-from-node-js

这是我的网络服务器节点 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';
    else
        console.log(request.url);
    var extname = path.extname(filePath);
    var contentType = 'text/html';
    switch (extname) {
        case '.js':
            contentType = 'text/javascript';
            break;
        case '.css':
            contentType = 'text/css';
            break;
    }

    fs.exists( filePath, function (exists) {
        console.log(filePath);
        if (exists) {

            fs.readFile(filePath, function (error, content) {
                if (error) {
                    console.log(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);

我的问题。

  1. 这没有显示 index.html。
  2. 如何发送基于不同请求的响应或 html 文件?例如:http://localhost:8125-> index.html http://localhost:8125/firstpage.html-> firstpage.html
  3. 我们是否需要将 html 文件放在包含 server.js(Web 服务器)的同一目录中?

我也在 cloud9 ide 中尝试过同样的方法。

在此处输入图像描述

我在 SO 中发现了类似的问题。但我没有明确的想法如何解决它。 Node.js - 基本节点 Web 服务器未提供 Socket.io 客户端文件 提前致谢。

4

1 回答 1

1

1-是的,是的。我可以重现错误。我不熟悉教程,但显然该行filePath = '/index.html';应该是filePath = './index.html';.

否则,服务器会在文件系统的根目录中查找“/index.html”,即一个名为“index.html”的文件,而不是在当前目录中。也许它在 Windows 操作系统上工作,但在 *nix 上这将不起作用。我添加了.index.html 。

2-为此,您需要导入“url”node.js 模块,然后您可以使用一种方便的方式来解析 URL:http ://nodejs.org/api/http.html#http_request_url 基本上,在该fs.exist ...行之前,您可以添加以下内容:

var requestedfile = urlparser.parse(request.url).pathname;
console.log("requested URL path name : " + requestedfile);

然后您将能够处理不同的请求。

3-是的,你可以。请记住,当添加一个点“。” 在问题 1 的“index.html”之前,您提供了 index.html 的相对路径(相对于 server.js 在文件系统上的位置)。因此,您可以通过这种方式指定任何其他相对路径。

如果您将页面存储在当前文件夹之外的另一个目录中(例如 /var/www/mysite),您仍然可以通过连接包含此绝对路径的字符串变量和请求路径名称来提供这些页面。例如(尽管经过测试)

var pagestorage = "/var/www/mysite/";
(...)
var pathname = urlparser.parse(request.url).pathname;
var requestfile = pagestorage + pathname;

这是更新文件的完整示例:

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

http.createServer(function (request, response) {

    console.log('request starting...');

    var filePath = "."+request.url;
    if (filePath == './')
        filePath =  './index.html';
    else
        console.log(request.url);
    var extname = path.extname(filePath);
    var contentType = 'text/html';
    switch (extname) {
        case '.js':
            contentType = 'text/javascript';
            break;
        case '.css':
            contentType = 'text/css';
            break;
    }

    // documentation at : http://nodejs.org/api/http.html#http_request_url
    var requestedfile = urlparser.parse(request.url).pathname;
    console.log("requested URL path name : " + requestedfile);

    fs.exists( filePath, function (exists) {
        console.log("looking up : " + filePath);
        if (exists) {

            fs.readFile(filePath, function (error, content) {
                if (error) {
                    console.log("error" + 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);

高温高压

于 2013-02-16T15:54:39.173 回答