NodeJS错误EADDRINUSE和HTML文件没有呈现,而是显示HTML代码

我是与NodeJS块新的孩子。 现在我正在学习一个NodeJS的基本教程,到目前为止这么好。

但我有一个问题,使用fs.createReadStream方法:

 var http = require("http"); var fs = require("fs"); function fourOHfour(response) { response.writeHead(404, {"Content-Type": "text/plain"}); response.write("four oh four....."); response.end(); } function onRequest (request, response) { if(request.method == 'GET' && request.url == '/'){ response.writeHead(200, {"Content-Type": "text/plain"}); fs.createReadStream("./index.html").pipe(response); } else{ fourOHfour(response); } } http.createServer(onRequest).listen(8888); console.log("server is running......"); 

当我在浏览器上inputlocalhost:8888 ,应该是在HTML中渲染index.html文件,但结果是错误的,我得到的只是一堆index.html文件的代码 – 纯文本。

错误localhost:8888

同时在我的崇高编译器中,对于这种情况我没有任何错误。 直到我试图编辑我的代码,无论我cahnge,它会给我这样一个错误:

错误崇高的编译器

如果这件事发生,我不能修复错误,除非我重新启动笔记本电脑,然后一切运行良好。 至less我的编译器说,服务器正在运行…甚至认为我的localhost:8888仍然不呈现HTML文件。

EADDRINUSE – 似乎端口正在被另一个进程占用,或者可能是由于不想closures的同一个nodejs进程。

试图杀死进程:

 killall node 

关于代码 – 试试这个:

 var http = require("http"), fs = require("fs"), URL = require('url'); function output(response, body, status) { status = status || 200; response.writeHead(status, {"Content-Type": "text/html"}); response.end(body); } function fourOHfour(response) { output(response, 404, "four oh four..."); } function onRequest (request, response) { var uri = URL.parse(request.url).pathname; // extractin URI part var method = request.method || 'GET'; // detecting method otherwise GET if(method == 'GET' && uri == '/'){ // reading file fs.readFile(__dirname+'/index.html', function(err, data) { if(err) { // if error happen, output error output(response, err, 500); return; } output(response, data); // ouput html body }); return; } fourOHfour(response); } var httpServer = http.createServer(); httpServer.on('request', onRequest); httpServer.listen(8888); 

要在生产中运行您的代码,请在terminal中执行

  1. 永远安装:

    sudo npm install -g forever#删除sudo word如果你使用windows,或者你已经是root用户

  2. 启动应用程序永远使用

    永远启动app.js

在开发环境中运行你的代码:

  1. 安装nodemon:

    sudo npm install -g nodemon#删除sudo word如果你使用windows,或者你已经是root用户

  2. 使用nodemon运行您的应用程序:

    nodemon app.js

永远将保持您的应用程序运行,并将输出日志,你可以看到使用:

 forever list # lists running processes forever logs # shows logs that outputs forever forever logs 0 # read logs of 0-th process 

永久重启进程:

 forever restartall # restarts all forever instances forever restart 0 # restarts first process 

停止:

 forever stopall forever stop 0 

关于Nodemon:它是一个监视文件中的变化并自动重启的工具,不需要停止启动你的应用程序,所以这就是为什么我更喜欢在开发环境中的nodemon

您正在将您的内容types指定为: text/plain ,这意味着页面不会以HTML格式呈现,而是以纯文本格式显示。 这就是为什么你从HTML文件中看到“代码”而不是正在呈现的HTML。

要解决这个问题,请将内容types设置为text/html如下所示:

response.writeHeader(200, {"Content-Type": "text/html"});

关于你发布的错误,“EADDRINUSE”

EADDRINUSE表示listen()尝试绑定服务器的端口号已被使用。

所以,在你的情况下,必须已经在端口8888上运行服务器。 检查这样的监听事件,看看服务器是否真的在监听:

 var http=require('http'); var server=http.createServer(function(req,res){ res.end('test'); }); server.on('listening',function(){ console.log('ok, server is running'); }); server.listen(8888);