我尝试使用 Express 监听端口:
const http=require('http');
const PORT_=process.env.PORT||7000;
const express=require('express');
const app=express();
app.use((req,res,next)=>{
res.send("Hellow world"+PORT_);
next();
});
app.listen(PORT_,(err)=>{
err===true ? console.log(err):console.log('run on '+port_);
});
当我在任何端口上运行程序时,出现以下错误:
node:events:498
throw er; // Unhandled 'error' event
^
Error: listen EACCES: permission denied :::7000
Emitted 'error' event on Server instance at:
at emitErrorNT (node:net:1357:8)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
code: 'EACCES',
errno: -4092,
syscall: 'listen',
address: '::',
port: 7000
当我使用http模块时,程序运行正常。
您收到错误是因为您尝试侦听的端口已在使用中。尝试侦听与您在代码中使用的端口不同的端口。
还有一种情况是,在 .env 文件中,您必须在声明变量后使用 semicolan(;)。虽然不必这样做。 例子:- 在 .env 文件中
PORT=5001; //this wrong
PORT=5001 //this is correct