0

I'm developing my application using Express.js but I don't want to use hostname like 'http://localhost:3000'.

How can I change that to another hostname and remove a port for my local environment only? Such as 'http://example.com' that point to my express's server.

jonrsharpe
  • 99,167
  • 19
  • 183
  • 334
pors
  • 139
  • 3
  • 11
  • you can tell the server to listen on `0.0.0.0`, on a server, this will be accessible via the url of the website – mast3rd3mon Jan 21 '19 at 14:54
  • also, if want to change the port number pass `PORT` in env, check before if already taking from env or not, otherwise, show the code for the server starrting segment. – Saikat Chakrabortty Jan 21 '19 at 14:56
  • That depends entirely on how you deploy it, `localhost:3000` is just for the dev server. You'll probably find that you still need to bind to a local port when you deploy, then the container/application runtime/whatever will expose it to traffic/routing. Maybe look into something like Heroku as a simple start. – jonrsharpe Jan 21 '19 at 14:57

1 Answers1

0

You can change your hosts file.

For change you can read this article

  • That is correct, but only half of the story. He needs to bind express on port 80 so he can "remove the port" – darklightcode Jan 21 '19 at 15:08
  • Now I added this to hosts file '127.0.0.1:3000 test.co' and it is working if I visit 'test.co:3000'. But how I remove port? I have changed my express server to listen on port 80 but it is error like this. events.js:167 throw er; // Unhandled 'error' event ^ Error: listen EADDRINUSE :::80 at Server.setupListenHandle [as _listen2] (net.js:1286:14) at listenInCluster (net.js:1334:12) at Server.listen (net.js:1421:7) – pors Jan 21 '19 at 15:18
  • You have application already run in port 80 ? you can see running port in : https://stackoverflow.com/questions/48198/how-can-you-find-out-which-process-is-listening-on-a-port-on-windows – Maxime Pomier Jan 21 '19 at 15:24
  • @pors in express `app.listen(80, '127.0.0.1')` and in hosts you add `127.0.0.1 test.co` (you don't add IP:PORT format in hosts file). EADDRINUSE means that the ip:port you're trying to listen to is already in use by another application, in this case you're using port 80 so applications like xampp, wamp, **skype** must be stopped because they use port 80 by default. – darklightcode Jan 21 '19 at 15:29