What Is Node.js?
According to Node.js, Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine. It uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.
From Google ImageBasically, Node.js is a way for us to write JavaScript code on the server side instead of browser or a separate JS file.
Why Should We Know Node.js?
1. The trendiest IT techonology for people to be learning right now
2. The largest ecosystem of open source libraries in the world.(From Node.js)
3. High performance
4. Write in JavaScript
Interact With Node Console
The Node console is just a place that we can open in our terminal with a simple command, and then we can start typing JavaScript commands. Then, they will be executed just like they are in the browser.
In the Goormide terminal, I type “node”, it will automatically switch to node system. Then, I type a simple console.log and enter, it print out the console.log message.
However, Node can’t run alert because alert can be only run by browser. Node is a server side where is not a browser. By the way, Document and Listener are not run in Node, too.
Run A File With Node
We can create a JavaScript file named “Test.js” by using Touch command. Then, in this file, we write a simple JavaScript for loop. Basically, when I run the code, it should print out “Hello Test” 10 times.
We want to execute the code in the command line without a browser or HTML or CSS. To use it, we just need to type node and then a file name:node Test.js.
Then, hit enter, it executes the file for us and print the message 10 times.
In the course exercise, the instructor let us to create a new file “echo.js”, and then write a echo function inside the file. The function is about to print out the string in certain number of times. For example, echo (“Echo”, 10) means print out “Echo” 10 times.
As we save the file and type “node echo.js”, the Node runs the code and print out the outcomes.
网友评论