This is a quick node project template for demoing Codespaces. It is based off of the Azure node sample and Haikus for Codespaces by Allison Weins.
- Start quickly with github.dev: a fast, free and lightweight VS Code editor in the browser
- Enjoy git integration to create branches and pull requests
- Continue working on Codespaces to run and debug
- Share your running app by forwarding ports
- Extend your code with GitHub Copilot
Before presenting this demo, be sure to create your own repository from the template, following these instructions:
-
Generate your repository in your organization, by clicking here.
This will open in a new tab where you can fill out the name and other details
-
Name your repository and continue
It's a familiar story ...
You just received an urgent request from your project stakeholders to fix the web app. You need to jump directly into the project and correct a mistake where the site is selling a bag of a 100 lemons when it's only supposed to be 10 per bag!
Compounding the urgency, you need to make the changes and you don't have the local development environment with Node.js and all of the required project dependencies. Not to worry though as you know you can use github.dev in the browser to make these changes.
-
Browse to the repository on GitHub at https://github.com/johnpapa/shopping-for-codespaces.
-
Press
.
and the code instantly opens in github.dev, a fast and free lightweight VS Code editor in the browser.Now you want to create your changes in separate branch and generate a new pull request.
-
Press
<F1>
to open the command palette. -
Type and select
Create New Branch
and name itfix/products
-
Press
<ENTER>
and selectSwitch to Branch
andLeave Site
to refresh to the new branch. -
Open the file containing the products in
products.json
. -
Edit the description for lemons to the proper quantity of 10 per bag.
"description": "Bag of 100 ripe lemons",
to
"description": "Bag of 10 ripe lemons",
-
This is a simple change so you're comfortable to commit the file changes.
While you're editing the
products.json
file you are communicating with your stakeholders to let them know the change is being made. Since you're in there, they ask if you can add a new entry for a new product, hot peppers! Of course you can do this, but you'll feel more confident if you can build, run and view the app to verify that it works. You decide it's time to run and debug the app.
-
While in the same branch, open the file containing the products in
products.json
. -
Add the missing product for hot peppers and save the file.
{ "id": 40, "name": "Hot Peppers", "description": "8 oz package of hot peppers", "quantity": 1, "imageClass": "fas fa-pepper-hot fa-3x" }
-
Now commit these changes.
-
You want to see the application run to make sure the hot peppers are displayed, so you press
<F5>
to run and debug the app.So far we've been working entirely with the files in the browser. But now you need compute power to run the app and debug it. You can seamlessly transition from github.dev to the full power of compute in the cloud. Let's see how.
-
Press the button
Continue Working On ...
and then selectCreate New CodeSpace
to be transitioned to Codespaces.The browser refreshes and you're now working with compute resources which allow you to run, debug, and use the terminal all with the environment you need in the cloud. It even ran
npm install
to install your dependencies! Let's debug your app.Note: You're using the default devcontainer for Node.js. You can create a custom devcontainer, by following the link in the terminal comments. But that's for another day.
-
Let's open the file
index.js
and set a breakpoint on the line that renders the products. -
Press
<F5>
to debug the app.Codespaces recognizes this as a JavaScript application and prompts you for the debugger profile.
-
Select
Node.js
as the profile and the debugger launches the application. -
Codespaces prompts you to open the browser to see the running web application, so you press the button
Open in Browser
, which launches the app.You hit the breakpoint you set in Codespaces. You can now debug your application and inspect and change variables.
-
Remove the breakpoint and let the debugger continue to render the webpage.
You notice your hot peppers have been added! Now you want to show the changes to your stakeholders before your merge to the
main
branch, so they can test it and confirm it works for them. You can do this by making your forwarded ports public.
-
Select the
Ports
tab in the Terminal pane. -
Notice the visibility is set to
private
by default. You will right click and selectPort Visibility
andPublic
Setting the port to public allows your stakeholders to see the app from the URL.
-
Now the URL can be shared with your stakeholders.
Your stakeholders review the running app and they're quite pleased ... until they notice that the products are not sorted in the proper ascending order by name. Uh oh, you're going to have to fix that! But you can't remember the exact syntax to sort the JavaScript array. Maybe GitHub Copilot can help you do this.
-
Stop the debugger.
-
Press
<F1>
to open Codespaces' command palette. -
Select
View: Show Extensions
-
Search for
GitHub Copilot
, pressInstall
, and agree to the terms -
Open the file
index.js
and put the cursor directly before therender
code. -
Type
//
to engage with Copilot -
Type
// sort the products by name in ascending order
, and hit<TAB>
and<ENTER>
to accept each line. -
You're prompted line by line for the code to sort the products. It looks appropriate, so click
<ENTER>
on each line to accept it, until it is complete. -
Comment the old line of code to render the products, now that you have replaced it.
app.get('/', (req, res) => { // sort the products by name in ascending order let sortedProducts = products.sort((a, b) => { if (a.name < b.name) { return -1; } if (a.name > b.name) { return 1; } return 0; }); res.render('index', { products: sortedProducts }); // res.render('index', { products }); });
-
Press
<F5>
to debug. -
Open the browser and see your changes worked!
Copilot just helped you write the code to sort the products directly in Codespaces in the browser.
- Stop the debugger.
- Commit your changes and push them to the branch.
- Create a new Pull Request.
- Review, approve, and merge the pull request to the
main
branch.
Let's look back at what we were able to accomplish, all without having the local dev environment. You can edit files, create a new branch, and merge a PR all from github.dev in the browser. Then when you need a robust dev invironment to run your application, you can continue working on it in Codespaces. Here you can debug it and use tools like GitHub Copilot to help write the code your app needs, all from within the browser.