I use Laravel valet as my local server and with that, it takes my project folder names and uses them as local domain names. So if I make a folder called test-website, I can now access that in the browser by going to test-website.test.
Starting up a node app, the only way I can access the app in the browser is by going to localhost::3000. That's fine and it works but I'd much rather a custom domain name e.g. new-node-app.test. Is there any way to do this and even better, is there any program out there that can automate this like Laravel Valet does?
You can use Laravel Valet to create a proxy.
valet proxy [--secure] [--] <domain> <host>
so you could do something like this to make https://node.test work as expected.
valet proxy --secure node.test http://127.0.0.1:3000
Note: proxies are forwarding requests to an existing service. So do not point your proxy to https://127.0.0.1:3000 unless you are already serving https.
Works with subdomains too. So if you were running a socket server, you may prefer to use subdomain of io or something.
valet proxy --secure io.mydomain.test http://127.0.0.1:3000
to verify you have the correct configuration. You can list proxies using valet proxies or delete a proxy by using valet unproxy
NEW ANSWER:
Use Valet proxy. In your terminal: valet proxy your.node.domain http://localhost:3000/
OLD ANSWER:
Say you want your domain name to be node.test. You have to create a file named node.conf in ~/.valet/Nginx (or ~/.config/valet/Nginx in newer versions).
node.conf :
map $sent_http_content_type $expires {
"text/html" epoch;
"text/html; charset=utf-8" epoch;
default off;
}
server {
listen 80; # the port nginx is listening on
server_name node.test; # setup your domain here
gzip on;
gzip_types text/plain application/xml text/css application/javascript;
gzip_min_length 1000;
location / {
expires $expires;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 1m;
proxy_connect_timeout 1m;
proxy_pass http://127.0.0.1:3000; # set the adress of the Node.js instance here
}
}
and then valet restart.
source: Valet Github: is it possible to share node.js program running on port 3000
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With