Can I run a web server in a GitHub Codespace?

13.2k Views Asked by At

Part of my development process involves using Mocha and Chai tests. I have a page in my test folder that loads all the code and tests, and I can start up a local node script that runs a simple local server, visit that page on http://localhost:8080/blahblahblah.html, and see the test results.

GitHub recently announced Codespaces and I signed up for the beta. If I start developing in a Codespace, I know there's a terminal there. If I run my testing server in that Codespace, how would I see the test results? Is it even possible to connect to the server in the container from outside? What would replace the URL I show above?

3

There are 3 best solutions below

1
On BEST ANSWER

Found the answer here:

https://docs.github.com/en/codespaces/developing-in-codespaces/forwarding-ports-in-your-codespace

It's actually pretty surprising. You just have your web server app print a localhost URL to the console, and the Codespace automatically converts that to a clickable URL with the appropriate port-forwarding. Kind of a huge surprise, but also pretty cool.

Just tested and this works.

3
On

When an application running inside a codespace outputs a port to the console, Codespaces detects the localhost URL pattern and automatically forwards those ports. You can click on the URL in the terminal to open it in a browser. For example, if an application outputs http://127.0.0.1:3000 or http://localhost:3000 to the console, the log would automatically convert the output to a clickable URL for port 3000.

enter image description here

1
On

Just wanted to add up on the previous answers: if your local machine is already using that port by some other application, the codespace will not ask you whether you would like to run on a different port; instead, it just gives a clickable link: http://localhost:3000 .

This would just display the other web application instead of the one on codespace. So, you gotta make sure that the port is unused in your local first before running it in the codespace.