Exercises
Introduction
Create these exercises with Node.js without creating any real Node.js project. Create a just one or more JavaScript files to complete each exercises.
These exercises are optional to do, but they contain a lot of useful information about Node.js programming. Exercise solutions are available in the same page, but remember try to code them first by yourself.
Exercise 01 - Testing Node.js setup
Test that you have Node.js installed correctly to your computer. Create a one JavaScript file and use a console.log()
to print some text string to the console.
solution - try to code it by yourself
1 2 |
|
1 2 |
|
Exercise 02 - Own functions
Create a sum
, substraction
, multiplication
and division
functions which takes two parameters and return calculation result. Call those functions from your application and show results.
solution - try to code it by yourself
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
1 2 3 4 5 |
|
Exercise 03 - Own module
Create own calculator
module and move above functions inside it. Use your own made module in your main app and call functions.
solution - try to code it by yourself
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
1 2 3 4 5 6 7 8 9 10 |
|
1 2 3 4 5 |
|
Exercise 04 - JavaScript and looping - part 1
Create an array with words and make a sentence by looping an array. Print created sentence to the console.
For example:
- array with words: "Looping", "array", "is", "simple", "at, "Node.js!"
- will print "Looping array is simple at Node.js!"
solution - try to code it by yourself
1 2 3 4 5 6 7 8 9 10 11 12 |
|
1 2 |
|
Look: reduce
Exercise 05 - JavaScript and looping - part 2
Create a function which will take a array of number as a parameters. Function should return a calculated sum. Call your function from the main application and display a sum. Your sum function should return a zero, if your array doesn't has any numbers.
Try to use reduce in your solution.
solution - try to code it by yourself
1 2 3 4 5 6 7 8 |
|
1 2 |
|
Exercise 06 - Command line parameters
Create an application which will return a randomized integer values in an array.
Application will take a command line parameter in following formula:
1 |
|
Usage
will be printed if parameter values are incorrect. Note that the first argument is in the third position of the array process.argv[2]
.
Example:
1 2 3 4 5 |
|
solution - try to code it by yourself
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
1 2 |
|
Exercise 07 - Callback functions
First get familiar with a callbacks.
Create a following small application with a callbacks.
1 2 3 4 5 6 7 8 9 |
|
1 2 |
|
Now try to think what is happening, which order and why.
solution - try to code it by yourself
Line 1 myFunctionWithCallback will be defined
Line 5 myFunc will be defined
Line 7 myFunctionWithCallback will be called with parameters
Line 2 myFunc will be called with parameters
Line 5 myFunc will return 'Pizza with hame and cheese' string to Line 2
Line 7 myFunctionWithCallback will return value to result
Line 9 result will be printed to the console
Exercise 08 - Asynchronous programming
This exercise has an introduction to the asynchronous programming. See the below example to simulate asynchronous execution with callbacks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
Now try to think what is happening, which order and why.
solution - try to code it by yourself
Code will print the following to the console:
1 2 3 4 5 6 7 |
|
setTimeout
function will have different waitTime
s to simulate task taking time. Task2
will be finished first because it has the shortest waitTime
.
It is similar than I/O functions in Node.js, because there are some wait time before tasks are ready. In a real web app you don’t know exactly how long time parsing for example remote JSON data takes or read data from the database.
Exercise 09 - Handling files tutorial
The fs module enables interacting with the file system from the Node.js application.
To use this module:
1 |
|
Create input.txt
file and store Sample text line in the file.
string inside it.
1 2 |
|
You can read the file with readFile
function. (error, data)
defines a callback function which will be called after a file reading has been finished.
1 2 3 4 5 6 |
|
Above programming will print the following lines to the console. Note how Line 6 will be called before the callback function will be called and line 2 or 3 has been executed.
1 2 |
|
Note
fs
simply provides a wrapper for the standard file operations, like:
- fs.readFileSync() to read files synchronously
- fs.readFile() to read files asynchronously
- fs.watch() for watching changes in files
- fs.exists() for checking that file exist
- fs.open() for opening the file
- fs.stat() for getting file attributes
- fs.read() for reading chuck of data from the file
- fs.writeFile() for writing to file
- fs.close() for closing the file
Exercise 10 - Read a file and calculate a sum of integers
Create an application which reads a numbers from a text file and calculates a sum. Numbers are separated with commas in a text file.
1 |
|
solution - try to code it by yourself
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
1 2 |
|
Exercise 11 - Write a text to the file
Create an application which takes a string as a parameter and write string to the file.
1 |
|
1 2 |
|
Open your saved text file to see that saving has been done successfully.
solution - try to code it by yourself
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
1 |
|
1 2 |
|
1 2 |
|
Exercise 12 - JavaScript Ninja
Create a text file which describes your knowledge of different parts of the JavaScript. Figure out a few ones and just type those in the text file with the following format:
1 |
|
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Create an application which loads a text file and displays information to the console.
1 2 3 4 5 |
|
solution - try to code it by yourself
Haha... sorry there is no answer for this one. Code it by yourself ;-)
Exercise 13 - Events with EventEmitter
Much of the Node.js core API is built around an asynchronous event-driven architecture in which certain kinds of objects called emitters emit named events that cause function objects listeners to be called.
For instance: a net.Server object emits an event each time a peer connects to it; a fs.ReadStream emits an event when the file is opened; a stream emits an event whenever data is available to be read.
This we have noticed in earlier execises already - hopefully!
All objects that emit events are instances of the EventEmitter
class.. So, you can create a emitter.
1 2 |
|
These emitter objects expose an eventEmitter.on()
function that allows one or more functions to be attached to named events emitted by the object.
For example something_happened
event
1 2 3 |
|
and something_else_happened
event
1 2 3 |
|
At any point in the lifecycle of emitter objects, you can use the emit
function to emit any named event you want.
1 2 |
|
Will print:
1 2 |
|
Read more: EventEmitter
Ok and now the exercise
Let's say that Lady Gaga is keeping a concert. Create an application which displays a text OMG - Lady Gaga start singing!
when the application is launched. After a some point of time (for example after 5 seconds) display a text WOW - It was the best concert in my life...
to the console. Use EventEmitter in your solution.
Bonus: Show a NICE - fireworks!
text in every one second.
Example
1 2 3 4 5 6 |
|
solution - try to code it by yourself
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
Note
Node.js allows us to create and handle custom events easily by using events module and EventEmitters.
To learn more - here is a one good deep level example to follow: Using Event Emitters in Node.js @ digitalocean
Exercise 14 - Web Server Tutorial
You can create a web server with http
module.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Write above code and start your server.
1 2 |
|
Try it from your browser and you should see 'Hello World!' text in your browser.
Modify your code to return HTML content. You migth may need to stop and start your server again. Just click CTRL-C to stop server running and run your app again.
1 2 |
|
Exercise 15 - Web Server Counter App
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 |
|
Web page is refreshed, and application prints to document
1 |
|
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.
solution - try to code it by yourself
Haha... sorry there is no answer for this one. Code it by yourself ;-)