Загрузка страницы

Automating with Node.js - 001

Book on Amazon: http://amzn.eu/1dxeQp4
Leanpub: https://leanpub.com/automatingwithnodejs

Video based on the popular 'How I automated my job with node.js' article on Medium.

https://medium.com/dailyjs/how-i-automated-my-job-with-node-js-94bf4e423017

Node.js was designed by Ryan Dahl and released in May 2009. It’s a way to execute JavaScript code server-side as opposed to the traditional client-side approach.

Whenever you think of JavaScript, you usually think of using it in a web browser. This is referred to as client side scripting. You: open up a new browser window, make a request to your favourite web page where the HTML and CSS gets rendered, and then JavaScript files get loaded and executed to provide dynamic functionality on the page. All browsers have a JavaScript engine that interprets the JavaScript you execute in that browser.

A well known JavaScript engine supported by Chrome called V8 interprets JavaScript in the browser, and is also used by node. Once you’ve installed node onto your computer, when you open up a CLI and type node, you are interacting with the node executable installed on your machine. When you pass a JavaScript file to it, the node executable behind the scenes uses V8 to interpret the script.

The first thing we are going to do is to create a JavaScript file called hello.js. In this file we are simply going to pass our name as a string argument, and allow the node executable to return us some output saying hello. I know… advanced stuff, right? This will give us an understanding of how we start using node in the context of creating a CLI application.

The first line we are going to create will declare a constant called args, where we pass process.argv. Now, what is this process object we have here? And what is this argv value? The process object in the context of node is a global that provides information about, and control over the current Node.js process. We will go into more detail in another video on the process object, but for now, we just want to know about one of its properties called argv.

The argv property is an array that holds information about what was passed in the CLI. The first two elements by default are: the path to the node executable; and the second, a path to the JavaScript file you are interpreting. Anything else passed in the CLI such as a string argument will be pushed on to the end of the array as a new element. So if I did: node hello.js “Shaun”, then, “Shaun” would be the third element of the array.

We want to output back to the user their name when we decide to be friendly and greet them. How can we do this? We simply create a new constant called name, and assign it from the third element of the array. As we will not make any use of the first two elements in the array, we remove them by applying a slice operation. This means we only have one element left, and we assign the name as the first element of the array.

If the user does not pass a string with their name, we want to check this and return a message telling them that this is not a negotiation. We want your name, otherwise you are unfortunately not getting a greeting. We can check if they didn’t pass a string argument by checking if the value is undefined. If it is, then we log a message to the user and exit the process. If however, they have decided to be generous and provide their name, we simply log out our friendly greeting back to the user.

Once the script reaches the end of the execution, the process is then exited and removed from the Operating system’s currently running processes.

Видео Automating with Node.js - 001 канала SMKS
Показать
Комментарии отсутствуют
Введите заголовок:

Введите адрес ссылки:

Введите адрес видео с YouTube:

Зарегистрируйтесь или войдите с
Информация о видео
16 июля 2018 г. 0:27:41
00:04:41
Яндекс.Метрика