Download
Download
1. 1
1. node --version
Copied!
You should see output similar to this, though the versions may be different:
1. 1
1. v16.13.0
Copied!
1. 1
1. cd /home/project
about:blank 1/7
11/10/23, 4:42 PM about:blank
Copied!
4. Clone the git repository that contains the artifacts needed for this lab, if it doesn’t already exist.
1. 1
Copied!
1. 1
1. cd lkpho-Cloud-applications-with-Node.js-and-React/CD220Labs/http_server
Copied!
6. List the contents of this directory to see the artifacts for this lab.
1. 1
1. ls
Copied!
7. Check the content of index.js. This is the server side script we will run in the next section.
1. 1
1. cat index.js
Copied!
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
8. 8
9. 9
10. 10
Copied!
Alternatively, you can also view the content of index.js through the file explorer menu. It would appear like this.
about:blank 2/7
11/10/23, 4:42 PM about:blank
1. 1
1. node index.js
Copied!
Copied!
3. In the second terminal window, use the curl command to ping the application.
about:blank 3/7
11/10/23, 4:42 PM about:blank
1. 1
1. curl localhost:8080
Copied!
1. 1
1. Hello, World!
Copied!
4. To verify the same with browser window, click on the Skills Network button on the left, it will open the “Skills Network
Toolbox”. Then click the Other then Launch Application. From there you should be able to enter the port number the
server is running on, which is 8080 and launch.
A new browser window will open up as below. (Note: New browser window may not open up if you browser settings does not
allow pop-ups)
5. To stop the server, go to the main command window and press Ctrl+c to stop the server and stay in that terminal.
about:blank 4/7
11/10/23, 4:42 PM about:blank
1. 1
1. cat today.js
Copied!
1. 1
2. 2
3. 3
4. 4
Copied!
Alternatively, you can also view the content of today.js through the file explorer menu. It would appear like this.
2. Check the content of index-with-require.js. As you will observe, this script requires the module today whose content we
saw in the previous step.
1. 1
1. cat index-with-require.js
Copied!
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
8. 8
9. 9
10. 10
11. 11
about:blank 5/7
11/10/23, 4:42 PM about:blank
4. const requestListener = function (req, res) {
5. res.writeHead(200);
6. res.end(`Hello, World! The date today is ${today.getDate()}`);
7. }
8.
9. const port = 8080;
10. const server = http.createServer(requestListener);
11. console.log('server listening on port: ' + port);
Copied!
Alternatively, you can also view the content of index-with-require.js through the file explorer menu. It would appear like this.
3. In order to start the server, we run the index-with-require.js file with the node command.
1. 1
1. node index-with-require.js
Copied!
Copied!
4. In the second terminal window which you opened earlier, use the curl command to ping the application.
1. 1
1. curl localhost:8080
Copied!
1. Hello, World! The date today is Wed Oct 14 2020 14:56:42 GMT+1030 (Australian Eastern Standard Time)
Copied!
5. To verify the same with browser window, click on the Skills Network button on the left, it will open the “Skills
Network Toolbox”. Then click the Other then Launch Application. From there you should be able to enter the port
number 8080and launch.
about:blank 6/7
11/10/23, 4:42 PM about:blank
A new browser window will open up which show Hello World! along with the date and time in your time zone.
Challenge:
Make changes in index-with-require.js to greet the user depending on the time of the day.
Summary
Now that you have learnt how to run a server you are ready to create your very own Node.JS server.
Author(s)
Lavanya
Changelog
Date Version Changed by Change Description
2020-11-04 1.0 Lavanya Initial version created based videos
2022-11-18 1.1 K Sundararajan Updated instructions based on Beta testing feedback
about:blank 7/7