In a folder with the server, find the
server.js
file. In this file,
you can write JavaScript that will
be executed on the server.
This file contains an object with routes, the keys of which are addresses, and the values are functions that will be executed when visiting the corresponding pages with the browser.
Let's look at an example. Place the following code in this file:
export default {
'/test/': function() {
return 'hello world';
}
}
After that, stop the server by pressing
the Ctrl + C
keyboard shortcut
in the console. Then restart it with
the command:
node index.js
You can now access via browser at
http://localhost:8080/test/
.
As a result, our function will be
executed and the text that it
returns with return
will
be sent to the browser.
We can place any JavaScript code in this function. For example, let's write the following:
export default {
'/test/': function() {
let num1 = 1;
let num2 = 2;
return num1 + num2;
}
}
Make the /page1/
, /page2/
,
/page3/
address handlers. Call
them using your browser.