How to Code in NodeJS - DigitalOcean

[Pages:418] This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. ISBN 978-1-7358317-2-5

How To Code in Node.js

David Landup and Marcus Sanatan Editors: Timothy Nolan and Brian MacDonald

2020-12

DigitalOcean, New York City, New York, USA

How To Code in Node.js

1. About DigitalOcean 2. Introduction 3. How To Write and Run Your First Program in Node.js 4. How To Use the Node.js REPL 5. How To Use Node.js Modules with npm and package.json 6. How To Create a Node.js Module 7. How To Write Asynchronous Code in Node.js 8. How To Test a Node.js Module with Mocha and Assert 9. How To Create a Web Server in Node.js with the HTTP Module 10. Using Buffers in Node.js 11. Using Event Emitters in Node.js 12. How To Debug Node.js with the Built-In Debugger and Chrome

DevTools 13. How To Launch Child Processes in Node.js 14. How To Work with Files using the fs Module in Node.js 15. How To Create an HTTP Client with Core HTTP in Node.js

About DigitalOcean

DigitalOcean is a cloud services platform delivering the simplicity developers love and businesses trust to run production applications at scale. It provides highly available, secure, and scalable compute, storage, and networking solutions that help developers build great software faster. Founded in 2012 with offices in New York City and Cambridge, MA, DigitalOcean offers transparent and affordable pricing, an elegant user interface, and one of the largest libraries of open source resources available. For more information, please visit or follow @digitalocean on Twitter.

Introduction

Preface -- Getting Started with this Book

We recommend that you begin with a clean, new server to start learning how to program with Node. You can also use a local computer like a laptop or desktop. If you are unfamiliar with Node, or do not have a development environment set up, Chapter 1 of this book links to a tutorial that explains how to install Node for development on macOS, or an Ubuntu 20.04 system.

Programming using Node requires some familiarity with JavaScript, so if you would like to learn more about the language itself before exploring this book, visit the DigitalOcean Community's JavaScript section to explore tutorials that focus on using JavaScript in a browser.

Once you are set up with a local or remote Node development environment, you will be able to follow along with each chapter at your own pace, and in the order that you choose.

About this Book

Node.js is a popular open-source runtime environment that can execute JavaScript outside of the browser. The Node runtime is commonly used for back-end web development, leveraging its asynchronous capabilities to create networking applications and web servers. Node is also a popular choice for building command line tools.

In this book, you will go through exercises to learn the basics of how to code in Node.js, gaining skills that apply equally to back-end and full stack

development in the process. Each chapter is written by members of the Stack Abuse team.

Learning Goals and Outcomes

The chapters in this book cover a broad range of Node topics, from using and packaging your own modules, to writing complete web servers and clients. While there is a general progression that starts with installing Node locally and running small Node programs on the command line, each chapter in this book can be read independently of the others. If there is a particular topic or chapter that catches your attention feel free to jump ahead and work through it.

By the end of this book you will be able to write programs that leverage Node's asynchronous code execution capabilities, complete with event emitters and listeners that will respond to user actions. Along the way you will learn how to debug Node applications using the built-in debugging utilities, as well as the Chrome browser's DevTools utilities. You will also learn how to write automated tests for your programs to ensure that any features that you add or change function as you expect.

If you would like to learn more about Node development after you have finished reading this book, be sure to visit the DigitalOcean Community's Node.js section.

How To Write and Run Your First Program in Node.js

Written by Stack Abuse The author selected the Open Internet/Free Speech Fund to receive a

donation as part of the Write for DOnations program. Node.js is a popular open-source runtime environment that can execute

JavaScript outside of the browser using the V8 JavaScript engine, which is the same engine used to power the Google Chrome web browser's JavaScript execution. The Node runtime is commonly used to create command line tools and web servers.

Learning Node.js will allow you to write your front-end code and your back-end code in the same language. Using JavaScript throughout your entire stack can help reduce time for context switching, and libraries are more easily shared between your back-end server and front-end projects.

Also, thanks to its support for asynchronous execution, Node.js excels at I/O-intensive tasks, which is what makes it so suitable for the web. Realtime applications, like video streaming, or applications that continuously send and receive data, can run more efficiently when written in Node.js.

In this tutorial you'll create your first program with the Node.js runtime. You'll be introduced to a few Node-specific concepts and build your way up to create a program that helps users inspect environment variables on their system. To do this, you'll learn how to output strings to the console, receive input from the user, and access environment variables.

Prerequisites

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download