Skip to content

Exercises 5 : Node.js (16p)

Remember use JAMK/GitLab to return your exercises. Student need to test that exercise really work.

Exercise 01 - Install and test [2p]

Install Node.js to your computer and create JavaScript application, which displays your name to console.

Example:

1
2
> node 01-exercise.js
Kirsi Kernel

Exercise 02 - Modules [4p]

Create your own user module with following requirements:

  • The module should have a getName function which returns your name
  • The module should have a getLocation function which returns your current city of residence

The module should have a constant that defines your birthday in the format dd.mm.yyyy. Define all functions and constant value to be visible outside the module. Use the module in your Node application and use all functions and constants. Print the information to the console.

1
2
> node 02-exercise.js
Kirsi Kernel lives in Jyväskylä and was born on 01.01.1980.

Exercise 03 - Files [4p]

Create an application which reads a numbers from a text file and calculates a sum. Numbers are separated with commas in a text file. You can use for example a followin numbers: 1,3,4,6,10,12.

1
2
3
> node 03-exercise.js
Reading file and calculate a sum...
Sum is 36.

Exercise 04 - HTTP requests [6p]

Create a Node.js server application which can be used from the web browser.

Requirements:

  • Application should store a total requests made to this application.
  • Application will respond this request made integer value to the client.
  • Store count number to the text file.

Example:

Browser is used to http://localhost:3000/ and application prints to document

1
Request counter value is 1

Web page is refreshed, and application prints to document

1
Request counter value is 2

Note

Don't worry if you counter value is increased more than one by time. That is normal - your browser makes more than one call. Most browsers make a call to grab /favicon.ico for example.