简体   繁体   English

Node.js HTTP WebServer结束后写入错误

[英]Node.js HTTP WebServer Write after End error

I am new to node.js and have been trying to setup just a basic server to start with. 我是node.js的新手,并且一直在尝试仅设置一个基本服务器。

I have the code below running and I get a "Write After End" error. 我有下面的代码正在运行,并且出现“结束后写入”错误。

Socket.html is a basic html file that has a hello world string in the body. Socket.html是一个基本的html文件,其主体中包含hello world字符串。 There is literally nothing else in that file. 该文件中实际上没有其他内容。 I have tried using the "ReadFileSync" method and that throws up a whole new set of errors that I don't fully understand. 我尝试使用“ ReadFileSync”方法,但会引发一整套我不完全理解的错误。

I will appreciate any help on this. 我将不胜感激。 I am brand new to this so please go a little easy on me :) Thank you in advance! 我对此是全新的,所以请对我放轻松一些:)预先谢谢!

I have verified that the path is correct, and that the buffer does have the data. 我已验证路径正确,并且缓冲区确实包含数据。

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

var server = http.createServer(function(request, response){
console.log('Connection');

var path = url.parse(request.url).pathname.substr(1);
path = "\\" + path;

switch(path){
    case '/':
        response.writeHead(200, {'Content-Type': 'text/html'});
        response.write('hello world');
        break;
    case '\\socket.html':
        //console.log(path);
        fs.readFile(__dirname + path, function(error, data){
            if(error){
                response.writeHead(404);
                response.write("This domain is missing");
            }
            else{
                console.log(data);
                response.writeHead(200, {"Content-Type": "text/html"});
                response.write(data,"utf8");
            }
        });
        break;
    default:
        response.writeHead(404);
        response.write("This domain is missing");
        break;
}

response.end();
});

server.listen(8001); server.listen(8001);

I think when you read from the FileSystem async, the response.end() method is called before response.write() I would suggest using the following code instead: 我认为当你从文件系统异步读取时, response.end()方法之前调用response.write()我会建议使用下面的代码来代替:

var http = require('http'); 
var url = require('url'); 
var fs = require('fs'); 
var server = http.createServer(function(request, response) {
    var path = url.parse(request.url).pathname.substr(1);
    path = "\\" + path; 
    switch(path) {
        case '/':
            response.writeHead(200, {'Content-Type': 'text/html'}); 
            response.write('hello world'); 
            response.end();
        break; 
        case '\\socket.html':
            fs.readFile(__dirname + path, function(error, data) { 
                if(error) {
                    response.writeHead(404); 
                    response.write("This domain is missing"); 
                } else {
                    response.writeHead(200, {"Content-Type": "text/html"});
                    response.write(data, "utf8"); 
                } 
                response.end();
            }); 
        break; 
        default: 
            response.writeHead(404); 
            response.write("This domain is missing"); 
            response.end();
        break;
    } 
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM