Build a simple nodejs app and conect to a remote github repository
Introduction
This is a step-by-step guide to build a simple nodejs web app with an express server and connect to a remote github repository. I am using macOS so windows or linux users may have to adapt where necessary.
The web app just sends okay. but the focus on this article is to set up a connection with github.
To learn about deploying this app or any other node app to EC2 refer to my (next article).
Here is the github repo of the complete project.
Pre-requisites
- An IDE. I prefer VS Code
- node and npm on your local system: Check if they are installed using
node -v
andnpm -v
commands. If not found, then install using NVM - A github account
- Basic understanding of using VS Code and the command line
Build the nodejs App
Initiate the project
- Create an empty folder named simple-node-app on your computer and open it on vs code.
- Open the terminal on VS Code using command palette or pressing the
control
and~
keys together - Run the command
npm init -y
. This initiates the node project. The-y
tag accepts all defaults.
Now we should have
Create the express application
- Create an empty file named
index.js
in your root folder by running the commandtouch index.js
- Install Express using npm:
npm i express
. (This should create a file namedpackage-lock.json
and a directory namednode_modules
.) - Put the following code block in the
index.js
file:
/**
* Import the express module and initiate the express app
*/
const express = require("express");
const app = express();
/**
* Set up the app to return "Okay" for any get request
*
* app.get() is the method to set up the app to do certain actions
* upon a get request is sent to the app in the specified route.
*
* In this case we are using "*" indicating that the app should just
* send "Okay" as a response to any get request to any route on the
* app.
*/
app.get("*", (req, res) => {
res.send("Okay");
});
/**
* Set up the PORT
*
* app.listen() is a method to set up the app to listen to the
* specified PORT - meaning if we go to http://localhost:8080
* on our browser, this app responds. Here we are setting up the
* app to listen to PORT 8080 and once the app is running, it
* should log the given message on the console.
*/
const PORT = 8080;
app.listen(PORT, () => {
console.log(`Server live on port ${PORT} 🔥`);
});
Now the application is ready. test it out:
- Run
node index.js
on the terminal and we should seeServer live on port 8080 🔥
on our console. - Go to any browser and paste and go to the url
http://localhost:8080
and we should see "Okay".
Install and setup git
Install git
- Check if you already have git installed using
git -v
. - If not found, install git on your system globally using npm:
npm i git -g
.
Set up git for this project
Create a file named .gitignore
on your root folder and put the following in the file:
.DS_Store
node_modules
This file tells git to ignore the changes made to the files and folders mentioned here.
Next,
- On the command line enter
git init
- Check the status of changes by running the command
git status
. It shows you the changes made on the directory so far - Run the command
git add .
to stage all changes to commit - Run the command
git commit -m "Initial Commit"
. This commits all staged changes with the message "Initial Commit"
Connect with github remote repository
Setup SSH Connection to gihub
- Check if you already have an SSH Key, run the command
ls -al ~/.ssh
- Check the directory listing to see if you already have a public SSH key. By default, the filenames of supported public keys for GitHub are one of the following. id_rsa.pub / id_ecdsa.pub / id_ed25519.pub
if not, lets create the keypair:
- run the following command
ssh-keygen -t ed25519
and press enter to select the default location. - Next the prompt will ask you to enter a passphrase. Enter a passphrase, you need to remember this for future connections.
- Enter the same passphrase again to confirm
- [Optional] Add your SSH private key to the ssh-agent and store your passphrase in the keychain. Run
ssh-add --apple-use-keychain ~/.ssh/id_ed25519
Next, we need to add the public key to gihub
- Run the following command to copy the contents of the public key
pbcopy < ~/.ssh/id_ed25519.pub
(Or whatever is the path to your new public key) - Go to github > Settings > SSH and GPG Keys (URL)
- Click New SSH Key, select a title of your choice, select key type "Authentication Key" and add the contents of the public key in the text box then press Add SSH Key.
Now we are ready to connect to github using SSH.
Create empty gihub repository
- Go to github.com and login to your account
- Click on create new reporitory
- Name the reporitory "simple-node-app" or anything of your choice
- Select Private
- Keep "Add a README file" option unchecked
- Select .gitignore template none
- Select License None
- Click Create Reporitory
- Now you should see an ssh endpoint on the top which is something like
git@github.com:yourusername/simple-node-app.git
, copy this endpoint. - Run the following commands one after the other but replace the endpoint in the first command with the actual one that you just copied
git remote add origin git@github.com:yourusername/simple-node-app.git
git branc -m main
git push -u origin main
That's it. Now the repository should be available on github.