Can I run a web server in a GitHub Codespace?
Asked Answered
W

3

18

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?

Wrestling answered 12/9, 2020 at 15:48 Comment(0)
W
17

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.

Wrestling answered 16/9, 2020 at 0:1 Comment(0)
S
10

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

Stibnite answered 2/10, 2020 at 22:31 Comment(3)
Hi, Marnel. While this answer is correct, I had already discovered and added it as the accepted answer--sorry!Wrestling
Oh don't worry. It's ok. Should I delete it then ?Stibnite
I say keep it for the screenshot. :)Wrestling
S
2

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.

Suprematism answered 21/10, 2023 at 17:45 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Freehold

© 2022 - 2024 — McMap. All rights reserved.