0% found this document useful (0 votes)
11 views

Installing NodeJS and Running Your First Web App

Uploaded by

b.nomundari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Installing NodeJS and Running Your First Web App

Uploaded by

b.nomundari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignment 2: Hello NodeJS

1 Installing NodeJS on the Local Machine (Win-


dows)
Step 1: Download Node.js Installer
1. Open a web browser and navigate to the official Node.js website: https://nodejs.org.
2. On the homepage, you will see two versions of Node.js:

• LTS (Long Term Support): Recommended for most users for stability.
• Current: Includes the latest features but may not be as stable.
Choose LTS and click on the Windows Installer link to download the .msi
installer.

Step 2: Run the Node.js Installer


1. Once the .msi file is downloaded, double-click it to start the installation
process. 2. The Node.js Setup Wizard will open. Click Next to proceed. 3.
Accept the License Agreement: Read the terms, check the box to accept
the agreement, then click Next. 4. Choose Installation Folder: The default
installation folder is C:\Program Files\nodejs. You can change it if desired,
then click Next. 5. Custom Setup: Keep the default options and click Next.
6. Install Tools for Native Modules (Optional): You can check or uncheck
this option based on your needs. 7. Click Install to begin the installation. 8.
Once complete, click Finish.

Step 3: Verify Node.js Installation


1. Open Command Prompt:

• Press Windows Key + R, type cmd, and press Enter.


2. To check if Node.js is installed, type the following command and press Enter:
1 node -v
2

1
This will display the installed Node.js version. 3. To check if npm (Node Package
Manager) is installed, type the following command and press Enter:
1 npm -v
2

This will display the installed npm version.

Step 4: Update npm (Optional)


To update npm to the latest version, run the following command:
1 npm install -g npm

This will globally install the latest version of npm.

Summary
You have now successfully installed Node.js and npm on Windows. You can
start developing Node.js applications or install additional packages using npm.

2 Installing NodeJS on the Remote Machine (Linux)


Step 1: Install curl
Ensure you have curl installed by checking the version:
1 curl -- version

If curl is not installed, you may need to manually download the binary if
you don’t have root access.

Step 2: Download and Install NVM


Use curl to download and install NVM:
1 curl -o - https :// raw . g i th u b u s e r c o n t e n t . com / nvm - sh / nvm / v0 .39.5/
install . sh | bash

This will install NVM in your home directory under $HOME/.nvm.

Step 3: Update Your Shell Profile


Update your shell configuration to enable NVM:
For bash, run:
1 source ~/. bashrc

For zsh, run:

2
1 source ~/. zshrc

You can also add the following manually to the end of the file:
1 export NVM_DIR = " $HOME /. nvm "
2 [ -s " $NVM_DIR / nvm . sh " ] && \. " $NVM_DIR / nvm . sh "

Step 4: Verify NVM Installation


To verify that NVM was installed successfully, run:
1 nvm -- version

Step 5: Install Node.js Using NVM


To install the latest stable version of Node.js:
1 nvm install node

To install a specific version of Node.js (e.g., 16.14.0):


1 nvm install 16.14.0

Step 6: Verify Node.js Installation


Check the Node.js version installed:
1 node -v

Check the npm version:


1 npm -v

Step 7: Set a Default Node.js Version (Optional)


To set the default Node.js version:
1 nvm alias default node

Alternatively, to set a specific version:


1 nvm alias default 16.14.0

3
Step 8: Managing Multiple Node.js Versions
Switch between Node.js versions:
1 nvm use 16.14.0

List all installed Node.js versions:


1 nvm ls

Uninstall a Node.js version:


1 nvm uninstall 16.14.0

3 Creating a Node.js Web App with Express.js


and EJS
Prerequisites
- Node.js and npm installed. - VSCode installed. - GitLab account with an
empty project already created. - Port number will be provided for running the
app.

Step 1: Set Up Your Local Project in VSCode


1. Open VSCode. 2. Navigate to the directory where you want to create your
project:
1 cd path / to / your / directory
2

3. Initialize a Node.js project by running:


1 npm init -y
2

Step 2: Install Express.js and EJS


Install express and ejs by running the following command in the terminal:
1 npm install express ejs

Step 3: Create the Basic Project Structure


1. In your project folder, create the following structure:
1 app . js
2 views /
3 index . ejs
4

4
2. Create a new file called app.js. This will be the main entry point for the
application.

Step 4: Create the Express.js ”Hello World” App


In app.js, add the following code:
1 const express = require ( ’ express ’) ;
2 const app = express () ;
3
4 // Replace with port provided by your instructor
5 const port = process . env . PORT || 3000;
6
7 app . set ( ’ view engine ’ , ’ejs ’) ;
8
9 app . get ( ’/ ’ , ( req , res ) = > {
10 res . render ( ’ index ’ , { message : ’ Hello World from Express . js and
EJS ! ’ }) ;
11 }) ;
12
13 app . listen ( port , () = > {
14 console . log ( ‘ Server is running on http :// localhost : $ { port } ‘) ;
15 }) ;

Step 5: Create the EJS Template


In the views/ folder, create a file called index.ejs and add the following con-
tent:
1 <! DOCTYPE html >
2 < html >
3 < head >
4 < title > Hello World </ title >
5 </ head >
6 < body >
7 < h1 > < %= message % > </ h1 >
8 </ body >
9 </ html >

Step 6: Set the Instructor-Provided Port


To run the app on the port provided by your instructor, execute the following
command, replacing PORT NUMBER with the actual port number:
1 PORT = PORT_NUMBER node app . js

For example, if the provided port is 5000, run:


1 PORT =5000 node app . js

Visit http://localhost:5000 in your browser to see the app.

5
Step 7: Use VSCode Built-in Git GUI for Version
Control
1. Open the Source Control tab in VSCode (Ctrl + Shift + G). 2. Click
”Initialize Repository” to initialize Git. 3. Stage the changes by clicking the +
icon next to the modified files. 4. Commit your changes by writing a commit
message (e.g., Initial commit) and clicking the checkmark (). 5. Add the
GitLab repository as a remote by running the following command:
1 git remote add origin your - gitlab - repository - url
2

6. Push the changes to GitLab via the Source Control tab by clicking the three
dots, selecting Push, and pushing to the repository.

Step 8: View Your Code on GitLab


1. Open your GitLab project. 2. Verify that all your project files have been
successfully pushed.

You might also like