我使用node-cron定期运行程序。我运行了下面的代码,但是我得到了下面的错误,指出端口已经在使用中。
我从Windows10的命令提示符运行node.js程序。一旦我得到这个错误,我就关闭了命令提示符窗口,目的是结束所有正在使用的连接。并再次打开窗口并再次运行节点程序。但是我得到了同样的错误。
我应该只在这个程序中使用端口号3128。这个程序到目前为止没有任何问题,因为我已经多次运行它了。但是今天突然出现了这个错误。
我想知道为什么这个端口在程序结束后仍然在使用,并且想知道是否应该使用一些类似于node-cron.closePort()的代码显式关闭node-cron程序
sched_exec.js
"use strict";
const cron = require("node-cron");
const express = require("express");
const fs = require("fs");
const sync_module = require('./sync');
const logwriter = require('./logWriter/logwriter');
const app = express();
// schedule tasks to be run on the server
//const exec_timing = "0 2 * * *";
const exec_timing = "* * * * *";
cron.schedule(exec_timing, function() {
logwriter.info("start");
//do something
});
app.listen(3128);错误:
C:\inetpub\ftpfolder\syncSchedule>node sched_exec
events.js:292
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::3128
at Server.setupListenHandle [as _listen2] (net.js:1313:16)
at listenInCluster (net.js:1361:12)
at Server.listen (net.js:1447:7)
at Function.listen (C:\inetpub\ftpfolder\syncSchedule\node_modules\express\lib\application.js:618:24)
at Object.<anonymous> (C:\inetpub\ftpfolder\syncSchedule\sched_exec.js:21:5)
at Module._compile (internal/modules/cjs/loader.js:1137:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
Emitted 'error' event on Server instance at:
at emitErrorNT (net.js:1340:8)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
code: 'EADDRINUSE',
errno: 'EADDRINUSE',
syscall: 'listen',
address: '::',
port: 3128
}我做了netstat -ab。它说,node.exe是端口3128的连接的所有者。但是,我不认为有任何其他节点进程正在运行。
我还对任务管理做了一些研究。

它有两个node.js进程。但我不知道为什么。我应该在另一篇文章中问这个问题吗?
发布于 2020-09-10 18:55:27
您的计算机上的某个地方正在运行此程序的另一个实例或其他程序。
EADDRINUSE是一个操作系统错误,它通知您的app.listen(3128)线路无法侦听计算机上该端口(3128)上的传入连接,因为其他程序已在侦听。
您尝试停止正在收听的程序失败。
你怎么知道哪个程序在运行呢?当然,您可以使用任务管理器(ctrl-shift-escape)查看所有正在运行的程序。
您还可以打开一个cmd shell (以管理员身份),然后使用netstat -ab命令,-b选项将显示正在侦听的可执行文件。
https://stackoverflow.com/questions/63828085
复制相似问题