我对node.js和非常陌生,我想知道测试下面的代码类型(一般是)的最佳方法。现在,我正在作为一个netbeans node.js项目中的一个文件来完成这一切。我没有得到输出,输出应该是"HELLO SERVER“。
它通常编译时没有错误,但是没有输出,有时它说。
“抛出er;//未处理的‘错误’事件错误:侦听EADDRINUSE :8000”
我已经知道这个拒绝意味着什么,我认为当我点击run两次时,它就会这样做,因为然后端口被第一次运行所占用。
我已经在多个端口上试过了,它可以工作,但是没有输出.
我应该用别的方法测试这个吗?为什么没有输出?输出应该是"HELLO SERVER“谢谢。
var http = require("http");
http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
request.on("data", function (chunk) {
response.write(chunk.toString().toUpperCase());
});
request.on("end", function () {
response.end();
});
}).listen(8000);
var http = require("http");
var request = http.request({
hostname: "localhost",
port: 8000,
method: "POST"
}, function (response) {
response.on("data", function (chunk) {
process.stdout.write(chunk.toString());
});
});
request.end("Hello Server");
发布于 2019-02-21 23:00:57
这意味着您的端口8000
已经被其他进程使用了。该进程是,可能是服务器的一个较旧的实例,该实例仍在运行(未正确退出),该实例仍在运行端口。
尝试使用端口8000
查找进程并终止它
在Linux上
fuser -k 8000/tcp
在Windows上
netstat -ano | findstr :8000
// Find the value of the PID (last column to the right)
taskkill /PID {pid_value} /F
发布于 2019-02-21 23:01:44
我测试了您的代码,服务器启动得非常好。"throw er; // Unhandled 'error' event Error: listen EADDRINUSE :::8000"
意味着正在启动服务器的端口上已经有一个服务器或其他进程正在运行。更改端口号或停止服务
var http = require("http");
http.createServer(function(request, response) {
response.writeHead(200, {
"Content-Type": "text/plain"
});
request.on("data", function(chunk) {
response.write(chunk.toString().toUpperCase());
});
request.on("end", function() {
response.end();
});
}).listen(3000);
var http = require("http");
var request = http.request({
hostname: "localhost",
port: 3000,
method: "POST"
}, function(response) {
response.on("data", function(chunk) {
process.stdout.write(chunk.toString());
});
});
request.end("Hello Server");
https://stackoverflow.com/questions/54817540
复制相似问题