I have the following node.js server:
const main = () => {
log(`Starting server`)
const exit = signal => {
server.close(() => process.exit())
log('Server is closed.')
}
const server = http.createServer(handleRequest)
server.listen(process.env.PORT, process.env.HOST, () => log(`Server is listening on ${process.env.HOST}:${process.env.PORT}`))
process.on('SIGING', exit)
process.on('SIGTERM', exit)
process.on('uncaughtException', (err, origin) => {
log(`Process caught unhandled exception: ${err} ${origin}`, 'ERROR')
})
}
When I first run the server, I see:
Server is listening on 127.0.0.1:8080
(which is expected)
I kill the server with ctrl-c
Subsequent attempts to start the server output:
[ 2023-05-09T00:29:53.264Z | INFO ] Process caught unhandled exception: Error: listen EADDRINUSE: address already in use "8080" uncaughtException ERROR
I have tried the following:
lsofnetstatfusernpx kill-port 8080brew install htop- restarting the computer
specifics:
$ sudo lsof -i :8080
$ sudo lsof -t -i :8080
$ sudo lsof -t -i tcp:8080
$ sudo lsof -i tcp:8080
$ sudo lsof -i :8080 -v
$ sudo lsof > ~/temp_lsof.txt
$ sudo lsof -iTCP:8080 -sTCP:LISTEN
$ sudo netstat -tuln | grep 8080
$ sudo netstat -lnp | grep 8080
$ sudo netstat -tanl | grep 8080
$ sudo netstat -anp tcp | grep 8080
Each and every one of these commands resulted in no output whatsoever.
╭ ~/Desktop/my_api (express-postrgesql)
╰ % npx kill-port 8080
Could not kill process on port 8080. No process running on port.
╭ ~/Desktop/my_api (express-postrgesql)
╰ % node server.js
[ 2023-05-09T01:13:24.247Z | INFO ] Starting api server
[ 2023-05-09T01:13:24.249Z | INFO ] Process caught unhandled exception: Error: listen EADDRINUSE: address already in use "8080" uncaughtException ERROR
I can't seem to find a process listening on port 8080 and yet... node insists something is. Any suggestions?
EDIT:
Switched the PORT in .env to 8081 to test (something looks screwy with the fact that the port is in ""...):
╭ ~/Desktop/my_api (express-postrgesql)
╰ % node server.js
[ 2023-05-09T01:17:50.148Z | INFO ] Starting api server
[ 2023-05-09T01:17:50.150Z | INFO ] Server is listening on "127.0.0.1":"8081"
^C
╭ ~/Desktop/my_api (express-postrgesql)
╰ % node server.js
[ 2023-05-09T01:17:59.339Z | INFO ] Starting api server
[ 2023-05-09T01:17:59.341Z | INFO ] Process caught unhandled exception: Error: listen EADDRINUSE: address already in use "8081" uncaughtException ERROR
╭ ~/Desktop/my_api (express-postrgesql)
╰ % sudo lsof -i :8081
Password:
╭ ~/Desktop/my_api (express-postrgesql)
╰ %
The answer was in how I was creating the environment variables, specifically
process.env.PORT. Big thanks to @Phil for his helpful questions.If I say
process.env.PORT = "8080"inside my server file, everything works ok. I mistook this to mean that I had to wrap my env variables in quotes when inside.env:(Note - none of my other env variables in
.envwere wrapped in quotes, but I wasn't paying attention to those when I addedHOSTandPORTto.env)When I read the env variables from
.envvia:HOSTwas wrapped in extra quotes.I don't even really know how the server started in the first place... tbd on what
server.listen("127.0.0.1", "8080", ...)doesEDIT
RE "what happens if I pass a string to
server.listen()?stringtoserver.listen().process.env.PORT = "8080"in the server file itself, I was, somewhat obviously, settingprocess.env.PORTto astring- the string8080process.env.PORT="8080"in my.envfile, when I read the variable from the.envfile toprocess.env, I read it as astringas well - the string"8080"server.listen()performsparseInt()on the params expecting numbers - in the case of the string8080,parseInt()works fine.parseInt("8080")returnsNaNNOW: I am under the impression that if a non-parsable string is passed to
server.listen(), something should... not work. But I still saw the logged statements fromserver.listen()'s callback:Per node docs,
The last parameter callback will be added as a listener for the 'listening' event.- so the server does seem to be "listening".I will find out:
ctrl-ckills the server?server.listen()performsparseInt()on the arguments, resulting on aNaN, why doesn'tserver.listen()return an error?EDIT
when loading the erroneously double-double quoted PORT from .env (
PORT="8080"), inspecting the server object inserver.listen()'s callback shows the following:(so to question 3. above, I don't think
server.listen()is being passedNaNafter all)HOWEVER, if I correct this in .env (
process.env.PORT=8080), we get the following:address()has the shape described in Net server.address() docs.Also note:
if
.envcontainsPORT=8080, in my server file,typeof process.env.PORTisstring(it just so happens to be astringthat could be coerced into an integer)If I say, explicitly,
server.listen('fake', ...),server.listen()"works" andserver.address()is'fake'