4

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?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Jonathan P
  • 418
  • 5
  • 14

3 Answers3

2

Say you want your domain name to be node.test. You have to create a file named node.conf in ~/.valet/Nginx.

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
    }
}

source: Valet Github: is it possible to share node.js program running on port 3000

bar5um
  • 843
  • 1
  • 9
  • 21
2

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

wizardzeb
  • 1,546
  • 2
  • 14
  • 30
1

Okay I actually just worked it out myself.. haha

So in the hosts file I added:

127.0.0.1:80 nodeapp.localhost

Going to that address works and doesn't seem to conflict with my Laravel Valet domains.

To note I did try using the extension .test which is what I use for Laravel Valet websites but that didn't work for obvious reasons.

Mo.
  • 26,306
  • 36
  • 159
  • 225
Jonathan P
  • 418
  • 5
  • 14