Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 带有节点Js的基本web服务器issuw_Windows_Node.js - Fatal编程技术网

Windows 带有节点Js的基本web服务器issuw

Windows 带有节点Js的基本web服务器issuw,windows,node.js,Windows,Node.js,我是NodeJS的新手。我正在跟踪链接。但是它总是执行response.writeHead(404)因此我无法查看index.html 这是我的web服务器节点js代码 var http = require('http'); var path = require('path'); var fs = require('fs'); http.createServer(function (request, response) { console.log('request starting

我是NodeJS的新手。我正在跟踪链接。但是它总是执行
response.writeHead(404)因此我无法查看index.html

这是我的web服务器节点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);
我的问题

  • 这不显示index.html
  • 如何发送基于不同请求的响应或html文件? 例如:
    http://localhost:8125
    ->index.html
    http://localhost:8125/firstpage.html
    ->firstpage.html
  • 我们是否需要将html文件放在包含server.js(web服务器)的同一目录中
  • 我在Cloud9IDE中也尝试过同样的方法

    我在SO中发现了类似的问题。但我不知道如何修复它。
    提前谢谢。

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

    否则,服务器将在文件系统的根目录中查找“/index.html”,即名为“index.html”的文件,而不是在当前目录中。也许它可以在windows操作系统上运行,但像这样在*nix上运行是行不通的。如果添加
    ,则提供index.html

    2-为此,您需要导入'url'node.js模块,然后您可以使用一种方便的方式解析url: 基本上,在
    fs.exist…
    行之前,可以添加以下内容:

    var requestedfile=urlparser.parse(request.url).pathname
    console.log(“请求的URL路径名:+requestedfile”)

    然后你就可以处理不同的请求了

    3-是的,你可以。请记住,在第1期的“index.html”之前添加点“.”时,您给出了index.html的相对路径(相对于server.js在文件系统中的位置)。因此,可以通过这种方式指定任何其他相对路径

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

    以下是更新文件的完整示例:

     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);
    

     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);