With this video tutorial, lets learn about testing our code snippets using REPL, before implementing it into the actual node application. And then, we’ll show you how to add routes to your node server.
In this tutorial, we create 5 routes to the server: “/” to “Home Page” “/about” to “About Us” “/contact” to “Contact Us” /”satish” redirected to “Home Page” anything else to “Page Not Found”
Here we test our code snippets on the command prompt, before implementing it into our actual application. Working with node url module on command prompt, we learnt to make use of parse() method of url object to fetch the hostname, port and the pathname of the actual URL. We are interested in pathname – we’ll be using it in our node application.
Here we require url and http module. Using http object we create HTTP Server. Using url object we call parse method to parse and fetch the path from the user request URL.
Once we fetch the path, using conditional statements we display the corresponding message with appropriate response header information. We also illustrate 301 redirection
C:\>node
> 1 + 1
2
> var a;
undefined
> a = 10
10
> a + 5
15
> a
10
> ++a
11
> a++
11
> a
12
> "Satish "+"B"
'Satish B'
> function technotip() { return 101; }
undefined
> technotip();
101
>2
C:\>node
> 1 + 1
2
> var a;
undefined
> a = 10
10
> a + 5
15
> a
10
> ++a
11
> a++
11
> a
12
> "Satish "+"B"
'Satish B'
> function technotip() { return 101; }
undefined
> technotip();
101
>2
Here we can type in our valid code snippet and instantly get the results. Some of the things we can do are: arithmetic operations, string manipulations, pre and post increment decrement of numbers, method declaration and defining and calling the methods etc.
We could even require some built-in node modules and using its objects we can look at the methods and properties it provides. Command Prompt Console Window
Note: Node.js is built on the same Google’s V8 JavaScript Engine which is using by Google’s Chrome – thus providing the same speed and robustness to our node.js applications.