Exercise - Run and debug code
Now that you're connected to your SSH server, you're ready to start coding! You need to generate a basic Node application because you're tasked with developing an Express app for your agency.
In this exercise, you'll create a basic Node project and run and debug the project code, all while connected using SSH.
Create and run a Node.js application
To create and run your Node.js application, you'll need to run a series of commands in the terminal.
Open a new terminal by selecting Terminal > New Terminal in the taskbar. From the terminal, run the following commands to update the packages in your Linux VM and install Node.js:
sudo apt-get updatecurl -sL https://deb.nodesource.com/setup_lts.x | sudo -E bash -sudo apt-get install nodejs -yRun the following command to install the Express generator:
sudo npm install -g express-generatorRun the following command to create a new Express application called myExpressApp:
express myExpressApp --view pugOpen the application files by selecting Open Folder in the Explorer view. Select myExpressApp in the drop-down to open the folder in your Visual Studio Code window.
In the next prompt, select OK, which opens the folder containing your application files. If prompted, trust the workspace.
Open the terminal again and run the following command to install all of the application's dependencies:
npm installRun the following command to run the application:
npm startThe application will run on your VM's
http://localhost:3000. The next step will show you how to browse this application on your local machine.
Browse the application
Now that the application is running, you can use Port forwarding to browse the web app on your local machine.
With the app still running, run the Ports: Focus on Ports View command in the Command Palette.
Select the Forward a port button.
Specify port 3000, then press Enter.
The server now forwards traffic on port 3000 to your local machine; you can now browse to
http://localhost:3000to see the running web app.In the terminal, stop the app by pressing Ctrl + C.
Edit and debug the application
You can use Visual Studio Code's built-in features to edit and debug the application running on the remote machine.
Select the File Explorer in the left menu in Visual Studio Code and open the app.js file.
Set a breakpoint on line 10 of the file by clicking the gutter to the left of the line number. You'll see a red circle displayed.
In the Run and Debug view, select Run and Debug. If prompted, choose Node.js.
When the app runs, you'll hit the breakpoint. You can inspect variables, create watches, and navigate the call stack in the Debug view in the sidebar. You can control your debug session, like stepping line by line, using the Debug action bar at the top.
You can also edit the file just like you would if the code was located on your local machine. Begin to type
app., which triggers IntelliSense.
Congratulations! You successfully ran, edited, and debugged code that existed only on the remote machine.