Creating a Simple Server that sends the message, "Hello World" , using Nodejs

 




Introduction.

In our first task on our "Journey to transition into Tech through Frontend and Backend Web development program", we had the opportunity to set up our development environment by installing the necessary code editors such as VS Code, Ubuntu (a Linux package), and Node.js. I also had the opportunity to install Git and also create a GitHub repository account.


Task: In this article, I will be taking you on a journey on how to set up a simple server that sends the message "Hello World!" using Node.js. I will therefore use Git to push the codes to GitHub by linking my GitHub account.

Prior Knowledge: Express.js (a minimalist and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. Link: https://expressjs.com/)


Procedures

1. The git bash terminal was opened from my desktop using the right-click option on the mouse. As the pop-up window emerged, a folder was created using the command; make a directory (mkdir)

command: mkdir (folder_name). 

Another option is to create a folder from the Windows pop-up window by clicking the right-click option on the mouse.

2. Still within the git bash environment, the "call directory" command was issued;

command: cd (folder_name).  

This allows you to "work" within the folder (root directory).

3. This next command was used to initial VS code as it was the code editor for this app.

command: code . 

Another option is to open the folder created (root directory) manually in VS code by first initializing the app from the start menu on the PC.

4. As the VS code was initialized, the terminal was opened and the git bash terminal initialized from the available options.

5. Since Node.js was already installed on the PC, it was initialized using the command;

command: npm init

6. The command opens up a set of instructions that had options to type in "keywords", "author", "description", "app version", etc. This also creates a package.json file within the root directory on the VS code editor as well as the file (index.js).

7. Using the code package manager (npm) of Node.js you will use the following commands;

command: npm i (wait for it to finish loading)

                   npm install express (this installs the dependency list, Express.js in the root directory by 

                   adding a file to the package-lock.json file and also creates the node_modules).


An image of the VS code editor showcasing the completed commands and the index.js file for the server.

8. To create a file within the root directory in VS code using the git bash terminal, this command was typed;
command: touch index.js (this file was part of the initialization process in point no. 6)

9. Since Express.js is a dependency file already created, the Express.js file (link provided above.) was searched online and the codes were typed into the index.js file within the root directory of the VS code editor. 
Within the console.log line, the "words" were changed to portray what the server must display and the port used is 3000.

10. The server application was initialized in the bash terminal using the command;
command: node index.js. 
The "scripts" code in the package.json file was edited and this command was typed;
command: "dev" : "node index.js".

11. Using this link address, http://localhost:3000, the browser opens with the message "Hello World!"


An image of the server displaying the message "Hello World!" on the web browser.



Deploy to GitHub

1. The GitHub account was signed in online using the username and password. The add "Read me" section was unchecked.

2. A repository was created with the same name as the folder used in the VS code editor.

3. A folder with the name node_modules was ignored since it had a lot of files that were too big to be pushed to GitHub. 

4. To create a file within the bash terminal of the VS code editor, we use the command;
command: touch .ignore (With this file, the Node.js was copied into it).

5. The instructions on the GitHub repository page were followed accordingly to push the folder to Github.



Honorable Mentions.

Google: This search engine comes in handy. A tool that can be used to find great resources, codes, files, and every information needed by a web developer.



Thanks for your time.







Daniel Kwasi Kpeglo.

Physicist| SQL| Python| HTML| CSS| JavaScript| Looker| LookML| BigQuery| Power BI| Tableau| GCCP

GitHub Link: https://github.com/Daniel-Kwasi-Kpeglo



Comments

Popular posts from this blog

The DO's and DON'Ts of owning a Personal computer.

Does your business need a website?