Can't start Vite dev server on 443 without root, but native node server on 443 works without root - node.js

I was trying to use vite in my project and wanted to start the local dev server on 443 port (MacOS) and I'm presented with the following error:
error when starting dev server:
Error: listen EACCES: permission denied 127.0.0.1:443
at Server.setupListenHandle [as _listen2] (node:net:1415:21)
at listenInCluster (node:net:1480:12)
at GetAddrInfoReqWrap.doListen [as callback] (node:net:1629:7)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:111:8)
Now as per *nix systems, I should get this error as ports < 1024 need root access.
But my question is, how am I then able to listen on 443 port on a native node server?
const http = require('http');
const requestListener = function (req, res) {
res.writeHead(200);
res.end('Hello, World!');
}
const server = http.createServer(requestListener);
server.listen(443);
The code above works without any issue (also works with https module) on my system and without root access.
Can someone explain what is happening?

Related

How to fix Kotlin Reactjs tutorial error: listen EADDRINUSE

As part of this basic tutorial, I downloaded the code from this repo, and attempted to run it with the command ./gradlew run on an Ubuntu 20.04 environment.
This resulted in the following error:
> Task :browserDevelopmentRun
✖ 「wds」: Error: listen EADDRINUSE: address already in use 127.0.0.1:8080
✖ 「wds」: Error: listen EADDRINUSE: address already in use 127.0.0.1:8080
at Server.setupListenHandle [as _listen2] (net.js:1318:16)
at listenInCluster (net.js:1366:12)
at GetAddrInfoReqWrap.doListen [as callback] (net.js:1503:7)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:69:8) {
code: 'EADDRINUSE',
errno: -98,
syscall: 'listen',
address: '127.0.0.1',
port: 8080
}
The process seems to be the only one running on this port, and attempts to get rid of any other processes using port 8080 (sudo kill -9 'sudo lsof -t -i:8080', killall node, etc...) have no effect. The conflicting processes seem to both come from within the tutorial web app.
Changing the port using a .env file also did not work. No matter what port is set (8081, 3000, 83000, etc...), there is still the error of port 8080 already being in use.
Does anyone know a solution to this issue? Thanks!
So while I didn't figure out exactly which dependency was causing this issue, on trying out a couple of the pull request on the repository, I found that this one: https://github.com/kotlin-hands-on/web-app-react-kotlin-js-gradle/pull/11 fixes the problem. Hopefully, it will be merged into the main branch soon!

NodeJS - Error: listen EADDRNOTAVAIL: address not available

I am running a Node app that should be hosted on a local server.
At the moment, I am sending just a plaintext response.
const http = require('http');
var server = http.createServer(function(req, res) {
res.writeHead(200, {ContentType: 'text/plain'});
res.end("test");
});
When I listen to the localhost everything works fine and I am able to send the request from my browser.
server.listen(3000, '127.0.0.1'); // works fine, on the same machine
However, if I try to listen to a port on my LAN network by typing the router's IP, I get an error.
server.listen(3000, '192.168.0.1'); // causes an error
Error: listen EADDRNOTAVAIL: address not available 192.168.0.1:3
000
at Server.setupListenHandle [as _listen2] (net.js:1253:19)
at listenInCluster (net.js:1318:12)
at doListen (net.js:1451:7)
at process._tickCallback (internal/process/next_tick.js:63:1
9)
at Function.Module.runMain (internal/modules/cjs/loader.js:7
57:11)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
Emitted 'error' event at:
at emitErrorNT (net.js:1297:8)
at process._tickCallback (internal/process/next_tick.js:63:1
9)
[... lines matching original stack trace ...]
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
I have tried this with my public IP address unsuccessfully.
Is there any way to listen to a port on a LAN server so that I can send requests from any computer on the network?
Also, I would later like my application to run on any computer on any LAN network. How can I dynamically add my host?
Try this
server.listen(3000, '0.0.0.0');
you can access your node application from other machine
e.g
your lan ip is 192.168.0.101 then you can browse 192.168.0.101:3000 from other machine
I got this error, the issue was I had a VPN connected
Be careful with your IP.
Sometimes it changes. When I work with Node I have to check the ipconfig in Shell... I hope with that it could help if someone else have the same problem...
The right IP address actually turned out to be 192.168.0.104.
The IP address of a server machine should be used, not that of a router.

How to use app.listen(port, host) with public hostname?

Can I put public domain name as host argument in app.listen(port, host)?
I know my ip, and I have 3 domain names on this ip. domain1.com, domain2.com all on same ip. Now, I'm using switch in app.get('/') to routing request of different domain names. However, I'm wondering is there a way to route host when setting up app.listen.
I found the entry of app.listen([port[, host[, backlog]]][, callback]) is really vague on expressjs website. There's nothing about the "host" argument. I know it can be set to localhost or 127.0.0.1. But my code failed when I put public domain name in it.
Can anybody explain how the host argument works in app.listen? Thank you.
var express = require('express');
var app = express();
app.get('/', function(req, res) {res.send("hello")});
app.listen(3000, "my.domain");
Error code:
Error: listen EADDRNOTAVAIL: address not available myip:3000
at Server.setupListenHandle [as _listen2] (net.js:1281:19)
at listenInCluster (net.js:1346:12)
at GetAddrInfoReqWrap.doListen [as callback] (net.js:1485:7)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:65:10)
Emitted 'error' event on Server instance at:
at emitErrorNT (net.js:1325:8)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
code: 'EADDRNOTAVAIL',
errno: 'EADDRNOTAVAIL',
syscall: 'listen',
address: 'myip',
port: 3000
}
The server can't run.
Try to start server with hostname 0.0.0.0 and get access from other network points by your IP (check by ifconfig).
Find out the public ip of your public domain, then put it in the app.listen.
Example if your public ip is 1.2.3.4:
app.listen(3000,'1.2.3.4');

I'm trying to deploy a node server to heroku and I'm getting this error: throw er; // Unhandled 'error' event

I'm trying to deploy a node app to heroku and it looks like judging from other questions that heroku is dynamically assigning a port and it is somehow incompatible with something.
(I had to remove some details to post the question)
events.js:182
throw er; // Unhandled 'error' event
^
Error: listen EACCES 0.0.0.0:80
at Object._errnoException (util.js:1041:11)
at _exceptionWithHostPort (util.js:1064:20)
at Server.setupListenHandle [as _listen2] (net.js:1305:19)
at listenInCluster (net.js:1370:12)
at Server.listen (net.js:1466:7)
at Function.listen (/app/node_modules/express/lib/application.js:618:24)
at Object.<anonymous> (/app/index.js:9:21)
at Module._compile (module.js:573:30)
at Object.Module._extensions..js (module.js:584:10)
at Module.load (module.js:507:32)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! TFP#1.0.0 start: `node index.js`
npm ERR! Failed at the TFP#1.0.0 start script.
Problem
I had the same issue. I ran into this problem when I tried to set the port manually:
app.listen(80, function () {
console.log('Example blog app listening on port 80!')
});
This led me to the same EACCES error.
Solution
I was able to solve the problem by going back and looking through the Heroku docs where I found:
The command in a web process type must bind to the port number specified in the PORT environment variable. If it does not, the dyno will not start.
So, assuming you're using express, if you switch the port assignment to look something like this:
app.set('port', (process.env.PORT || 5000));
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});
Then you application should start properly.
Solution without Express.js
If you are not using Express, then you can set the port simply as:
var PORT = process.env.PORT || 5000;
Where process.env.PORT will be your production port, and 5000 will be the port you would use when testing your server locally.
I have faced this kind of problem. The exact error is the port 80 is already enabled in another project or app. So before start your app, stop the other app mapped to 80 port.
Now you can run your app without this error.
Thanks

Docpad and openshift

Has anyone been able to get docpad up and running on openshift paas? If so, how do you do it? I've configured the port in the environments section of the docpad.coffee file to "process.env.OPENSHIFT_NODEJS_PORT" and set the env to "production" - but to no avail. Trying to launch docpad from the server.js file just seems to get the application stuck in a loop of trying to start up and then closing down with an "EACCES" error.
Edit: Here's the error message
Error: listen EACCES
at errnoException (net.js:884:11)
at Server._listen2 (net.js:1003:19)
at listen (net.js:1044:10)
at Server.listen (net.js:1110:5)
at startServer (/var/lib/openshift/52deb1644382ec26f9000098/app-root/runtime/repo/node_modules/docpad/out/lib/docpad.js:4134:25)
at /var/lib/openshift/52deb1644382ec26f9000098/app-root/runtime/repo/node_modules/docpad/out/lib/docpad.js:4204:16
at TaskGroup.<anonymous> (/var/lib/openshift/52deb1644382ec26f9000098/app-root/runtime/repo/node_modules/docpad/out/lib/docpad.js:1109:14)
at TaskGroup.g (events.js:175:14)
at TaskGroup.EventEmitter.emit (events.js:98:17)
at TaskGroup.complete (/var/lib/openshift/52deb1644382ec26f9000098/app-root/runtime/repo/node_modules/docpad/node_modules/event-emitter-grouped/node_modules/taskgroup/out/lib/taskgroup.js:556:14)
at TaskGroup.itemCompletionCallback (/var/lib/openshift/52deb1644382ec26f9000098/app-root/runtime/repo/node_modules/docpad/node_modules/event-emitter-grouped/node_modules/taskgroup/out/lib/taskgroup.js:347:17)
at TaskGroup.EventEmitter.emit (events.js:95:17)
at Task.<anonymous> (/var/lib/openshift/52deb1644382ec26f9000098/app-root/runtime/repo/node_modules/docpad/node_modules/event-emitter-grouped/node_modules/taskgroup/out/lib/taskgroup.js:405:26)
at Task.EventEmitter.emit (events.js:117:20)
at Task.completionCallback (/var/lib/openshift/52deb1644382ec26f9000098/app-root/runtime/repo/node_modules/docpad/node_modules/event-emitter-grouped/node_modules/taskgroup/out/lib/taskgroup.js:128:19)
at DocPad.<anonymous> (/var/lib/openshift/52deb1644382ec26f9000098/app-root/runtime/repo/node_modules/docpad/out/lib/docpad.js:922:18)
at ambi (/var/lib/openshift/52deb1644382ec26f9000098/app-root/runtime/repo/node_modules/docpad/node_modules/ambi/out/lib/ambi.js:23:18)
at Task.<anonymous> (/var/lib/openshift/52deb1644382ec26f9000098/app-root/runtime/repo/node_modules/docpad/node_modules/event-emitter-grouped/out/lib/event-emitter-grouped.js:38:23)
at ambi (/var/lib/openshift/52deb1644382ec26f9000098/app-root/runtime/repo/node_modules/docpad/node_modules/ambi/out/lib/ambi.js:23:18)
at fire (/var/lib/openshift/52deb1644382ec26f9000098/app-root/runtime/repo/node_modules/docpad/node_modules/event-emitter-grouped/node_modules/taskgroup/out/lib/taskgroup.js:159:23)
at b (domain.js:183:18)
at Domain.run (domain.js:123:23)
at Task.fire (/var/lib/openshift/52deb1644382ec26f9000098/app-root/runtime/repo/node_modules/docpad/node_modules/event-emitter-grouped/node_modules/taskgroup/out/lib/taskgroup.js:166:25)
at processImmediate [as _immediateCallback] (timers.js:317:15)
I was able to reproduce this and it boils down to not being able to specify a listening address in the docpad.coffee. I've messaged the maintainers to see what can be done to change that.
As niharvey pointed out, we need to tell docpad to listen on a specific address.
In version 6.58 of docpad the "host" option was added to the environments section of the docpad.coffee file. So it needs to have the following section:
environments:
development:
# //Always refresh from server
maxAge: false # default
# //Listen to port 9778 on the development environment
port: 9778
production: # //this is the important part for openshift
# //this is the part that open shift needs -
# //but its only available in docapd 6.58+
hostname: process.env.OPENSHIFT_NODEJS_IP
port: process.env.OPENSHIFT_NODEJS_PORT

Resources