Content in this paper,

Node.js 10 has entered the LTS era! Its application scenarios have expanded from scaffolding and auxiliary front-end development (SSR, PWA, etc.) to API mid-tier, proxy tier, and professional back-end development. Node.js is also becoming more mature in enterprise Web development, both in the API middle layer, and in microservices have been very well landed. This book will take you to the main battleground of Node.js through the Web development framework Koa2! This book systematically explains the process and steps of using Koa framework to develop Web applications in practical projects. Chapter 1 introduces node.js installation, development tools, and debugging. Chapters 2 and 3 describe the prototype of the Koa field project. Chapter 4 details the basics of HTTP and its practical application. Chapter 5 covers MVC, template engines, and file uploads. Chapters 6 through 8 cover database, unit testing, and project optimization and deployment. Chapter 9 ~ 13 introduces the whole process of building the popular micro channel small program front end and background management application from scratch, as well as the final server deployment, including HTTPS, Nginx. This book is rich in examples, focusing on the actual situation, with a complete practical project throughout all chapters, and provides all the source code involved in the book and part of the supporting video tutorial, will be front-end developers based on the new field and back-end developers to understand Node.js and use Koa2 to develop Web applications right-hand man.

preface

Node.js was born in 2009 and will be nearly 10 years old by the time this book is published. It expands the scope of JavaScript and enables JavaScript to operate on various system resources just like other languages. As a result, a large number of tools for front-end engineering development begin to run in the Node.js environment. Because Node.js uses event-driven, non-blocking I/O, and asynchronous output to improve performance, a large number of I/ O-intensive applications are developed using Node.js. Mastering Node.js development can not only greatly broaden the technical knowledge of front-end developers, but also expand the living space of front-end developers, and stand out from the current environment of more and more front-end developers.

Because Node.js only provides basic class libraries, developers need to design the application architecture reasonably and call a large number of basic class libraries for development. In order to improve the development efficiency and reduce the development threshold, many Web frameworks based on Node.js have emerged in the relevant technical community.

The Express framework emerged at the beginning of Node.js and quickly became the mainstream Web application development framework. In the community, a large number of third-party developers have developed rich Express plug-ins, which greatly reduces the development cost of Web applications based on Node.js, and also drives a large number of developers to use Express framework to develop Web applications. But the Express framework handles asynchronous calls in a traditional callback fashion, making it easy for inexperienced developers to write code into “callback hell,” making the application difficult to maintain. Following the Generator function proposed in the ECMAScript 6 specification, Express author TJ Holowaychukgithub.com/tj cleverly developed the CO library github.com/tj/co to enable developers to use yield keywords, Developing asynchronous applications as if they were synchronous code solves the “callback hell” problem. In 2014, he developed a new generation of Web application development framework Koa based on CO library, which is described as “Next Generation Web Framework for Node.js” in the official language.

Community developers have developed a large number of plug-ins for Koa, and the processing mechanism is fundamentally different compared to Express. Whereas Express’s plug-ins execute sequentially, Koa’s middleware is based on the “Onion model” and can execute pre-request processing and post-request processing code in the middleware. ECMAScript 7 provides Async/Await keywords to better support asynchronous calls syntactically. TJ Holowaychuk released Koa version 2 (Koa2 for short) by replacing CO library with Async/Await to handle asynchronous callbacks based on Koa. With the release of Node 8 LTS (Long Term Support), which officially supports ECMAScript 7, more and more Node.js Web applications are developed using the Koa framework, which gradually replaces the Express framework. Despite the current popularity of Koa, the “all-natural” Node.js 8 support for ECMAScript 7 syntax was only officially released in October 2017. At present, there are few books on Koa in the market, and most of them are about the Express framework. This book is the first to introduce Koa. This book introduces the knowledge system that application development should have from node.js foundation, HTTP, Koa framework, database, unit testing and operation and maintenance deployment. By reading this book, you will be able to understand all aspects of Node.js development and reduce the problems of actual development. At the same time, the key chapters of the book also provide online code explanation and video, readers can read the book at the same time, combined with online code explanation and video, easier to understand the knowledge introduced in the book.

Special thanks to Du Keke, Ha Zhihui, Jiang Shuai, Li Bo, Li Yi, Sheng Hanqin, Tian Xiaohu, Xu Lei, Yan Meng and Zhao Chenxue (in no particular order) for the development and support of online training audio and video course resources.

This book features

  • Instructional videos are attached to key chapters. In order to make the book easier to understand, some basic and key topics are accompanied by video tutorials. You can visit https://ikcamp.com and watch the video in conjunction with the book.
  • All source code is hosted on GitHub. To make it easier for readers to access the source code, all the source code of the book is hosted on GitHub (github.com/ ikcamp), and readers can communicate directly with the author of the book through GitHub.
  • Summary of actual combat experience of Node.js technology stack of first-tier Internet companies. This book supplements back-end development skills and specifications that front-end developers don’t have. It explains how to develop Koa applications, how to read and write databases through Object Relational Mapping (ORM) libraries, and how to ensure code quality through unit testing. How to use PM2 and CI to start and deploy Node.js applications, and how to use logs and monitoring to ensure the stable running of online applications.
  • Typical project case analysis, practical strong. The third part of the book introduces the popular small program technology through the cloud album small program development project, including the small program login process, scan code login, file upload, album management and other functions. By learning the relevant content of this book, readers can independently develop popular applets and their need for back-end services.

Knowledge System of this Book

Chapter 1 Basic Knowledge (Chapters 1-4)

This section introduces the basics of developing Koa applications, including getting started with Node.js, Meeting Koa, routing, and HTTP. Chapter 1 introduces the history and development of Node.js, as well as the foundation and environment preparation of Node.js. NPM (Node Package Manager, a third-party Package management tool of Node.js) enables developers to easily use a large number of third-party software packages. This chapter also introduces Microsoft’s free development tool, the Visual Studio Code Editor, and how to use it to debug Node.js applications. Chapter 2 introduces the history of Koa and middleware as the core technology of Koa. Chapter 3 introduces the concept of routing and the most popular routing middleware in Koa, the KOA-Router. In Chapter 4, the basic knowledge of HTTP and its successor protocol HTTP/2 are introduced. This paper introduces how to get the data from the client in Node.js and how to get the body data in the request by koa-BodyParser middleware.

Chapter 2 Practical Application (Chapters 5 ~ 8)

This section introduces all aspects of application development, including four chapters: building Koa Web applications, databases, unit testing, optimization, and deployment. Chapter 5 covers MVC architecture, template engines, static resources, how to output JSON data, how to upload files through koa-Multer middleware, and more. In chapter 6, it introduces the concept of database and relational database represented by MySQL, and how to operate MySQL database through ORM class library. This paper introduces the non-relational database represented by MongoDB and how to operate MongoDB in Node.js. This paper introduces the new cache database represented by Redis, and how to use Redis to realize Session persistence in Node.js. Chapter 7 introduces the Chai assertion library, which detects whether results during unit testing are as expected. Introduces the Mocha testing framework, which allows you to write and run unit test code. This paper introduces how to use SuperTest tool to test HTTP service and simulate HTTP service request response by Nock library. Finally, Nyc tools were introduced to check unit test coverage and improve code quality. Chapter 8 shows you how to log and uniformly catch exceptions, and how to output custom error pages; Introduces how to launch applications through PM2 and Docker, how to release applications through CI integration, and how to provide HTTPS support through Nginx. This section describes how to use logs to monitor server running status and how to use Keymetrics provided by PM2 to monitor cloud servers.

Chapter 3 Actual project: Build wechat applet background from scratch (Chapter 9~13)

This part through the introduction of the most popular small program development, combined with the specific album small program to explain how to develop a complete small program, and how to deploy small program. Among them, summarizing the knowledge of the previous chapters of the book introduces the function modules of small program, interface development, small program development, management background development and service deployment. Chapter 9 introduces the product functions of applets and how to develop applets portal. In chapter 10, it introduces the logics and implementation of small program login, the interface used in small program and the interface needed by background management system. Specifically, it includes how to authenticate through middleware, how to uniformly control the permissions of background management system, how to define data model and access and store data through Mongoose, and how to use Log4JS to record logs. Chapter 11 introduces the process of developing wechat applets and how to develop applets with the help of wechat developer tools. Chapter 12 introduces the overall architecture and design idea of developing background management system, and provides a set of technical scheme of login and authentication. Chapter 13 introduces the online deployment process of applets related services, including the deployment of database, Nginx, HTTPS, and Koa services, including how to resolve multiple domain names to the same cloud server through Nginx, and how to manage applications through PM2.

This book is for the reader

  • Web front-end developer
  • Developers interested in Node.js applications
  • Node.js development self – learning
  • Teachers and students of relevant majors in colleges and universities
  • Trainees from relevant training institutions

This book is written by Chen Dafu, Jin Jing, Gan Jun, Zhang Litao, Dai Liang, Zhou Yao and Xue Shuying. There are a lot of technical knowledge points involved in this book. Although members of the author’s team strive to contribute good works so that the technology can be better popularized, there are inevitably omissions and deficiencies. If readers have any questions or suggestions, they can directly leave a message on iKcamp’s GitHub. Book on the source code can also be used for making available, address to https://github.com/ikcamp. Book part is equipped with video, can go to https://camp.qianduan.group/koa2/2/0/0 to watch.

In 2019, iKcamp’s original new book Koa and Node.js Development Actual Combat has been sold on JD.com, Tmall, Amazon and Dangdang!