100% found this document useful (2 votes)
34 views

Practical Node.js: Building Real-World Scalable Web Apps Azat Mardan pdf download

The document is a promotional text for various programming books authored by Azat Mardan, including 'Practical Node.js: Building Real-World Scalable Web Apps.' It provides links for downloading these books and mentions their contents, which cover topics like Node.js, Express.js, and web development. Additionally, it includes information about the author's background and the book's publication details.

Uploaded by

baisegrudaiq
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
34 views

Practical Node.js: Building Real-World Scalable Web Apps Azat Mardan pdf download

The document is a promotional text for various programming books authored by Azat Mardan, including 'Practical Node.js: Building Real-World Scalable Web Apps.' It provides links for downloading these books and mentions their contents, which cover topics like Node.js, Express.js, and web development. Additionally, it includes information about the author's background and the book's publication details.

Uploaded by

baisegrudaiq
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 67

Practical Node.

js: Building Real-World Scalable


Web Apps Azat Mardan pdf download

https://textbookfull.com/product/practical-node-js-building-real-
world-scalable-web-apps-azat-mardan/

Download more ebook from https://textbookfull.com


We believe these products will be a great fit for you. Click
the link to download now, or visit textbookfull.com
to discover even more!

React Quickly: Painless web apps with React, JSX,


Redux, and GraphQL 1st Edition Azat Mardan

https://textbookfull.com/product/react-quickly-painless-web-apps-
with-react-jsx-redux-and-graphql-1st-edition-azat-mardan/

Pro Express js Master Express js The Node js Framework


For Your Web Development Mardan Azat

https://textbookfull.com/product/pro-express-js-master-express-
js-the-node-js-framework-for-your-web-development-mardan-azat/

Full Stack JavaScript Learn Backbone js Node js and


MongoDB Mardan Azat

https://textbookfull.com/product/full-stack-javascript-learn-
backbone-js-node-js-and-mongodb-mardan-azat/

Building Isomorphic JavaScript Apps From Concept to


Implementation to Real World Solutions 1st Edition
Jason Strimpel

https://textbookfull.com/product/building-isomorphic-javascript-
apps-from-concept-to-implementation-to-real-world-solutions-1st-
edition-jason-strimpel/
React js Essentials A fast paced guide to designing and
building scalable and maintainable web apps with React
js 1st Edition Fedosejev

https://textbookfull.com/product/react-js-essentials-a-fast-
paced-guide-to-designing-and-building-scalable-and-maintainable-
web-apps-with-react-js-1st-edition-fedosejev/

Client Server Web Apps with JavaScript and Java Rich


Scalable and RESTful 1st Edition Saternos Casimir

https://textbookfull.com/product/client-server-web-apps-with-
javascript-and-java-rich-scalable-and-restful-1st-edition-
saternos-casimir/

React 16 essentials a fast paced hands on guide to


designing and building scalable and maintainable web
apps with React 16 Second Edition. Edition Boduch

https://textbookfull.com/product/react-16-essentials-a-fast-
paced-hands-on-guide-to-designing-and-building-scalable-and-
maintainable-web-apps-with-react-16-second-edition-edition-
boduch/

Building Web Apps with WordPress WordPress as an


Application Framework Brian Messenlehner

https://textbookfull.com/product/building-web-apps-with-
wordpress-wordpress-as-an-application-framework-brian-
messenlehner/

Practical Bot Development: Designing and Building Bots


with Node.js and Microsoft Bot Framework Szymon Rozga

https://textbookfull.com/product/practical-bot-development-
designing-and-building-bots-with-node-js-and-microsoft-bot-
framework-szymon-rozga/
Practical
Node.js
Building Real-World Scalable Web Apps

Second Edition

Azat Mardan
Practical Node.js
Building Real-World Scalable
Web Apps
Second Edition

Azat Mardan
Practical Node.js
Azat Mardan
San Francisco, California, USA

ISBN-13 (pbk): 978-1-4842-3038-1 ISBN-13 (electronic): 978-1-4842-3039-8


https://doi.org/10.1007/978-1-4842-3039-8
Library of Congress Control Number: 2018958762

Copyright © 2018 by Azat Mardan


This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the
material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation,
broadcasting, reproduction on microfilms or in any other physical way, and transmission or information
storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now
known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with
every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an
editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the
trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not
identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to
proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication,
neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or
omissions that may be made. The publisher makes no warranty, express or implied, with respect to the
material contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Louise Corrigan
Development Editor: James Markham
Coordinating Editor: Nancy Chen
Cover designed by eStudioCalamar
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street,
6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-
sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member
(owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a
Delaware corporation.
For information on translations, please e-mail rights@apress.com, or visit www.apress.com/
rights-permissions.
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and
licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales
web page at www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available to
readers on GitHub via the book's product page, located at www.apress.com/9781484230381. For more
detailed information, please visit www.apress.com/source-code.
Printed on acid-free paper
To Vladimir Nabokov and The Defense
Table of Contents
About the Author�����������������������������������������������������������������������������������������������������xv

Acknowledgments�������������������������������������������������������������������������������������������������xvii
Introduction������������������������������������������������������������������������������������������������������������xix

Chapter 1: Setting up Node.js and Other Essentials������������������������������������������������� 1


Installing Node.js and npm����������������������������������������������������������������������������������������������������������� 1
One-Click Installers����������������������������������������������������������������������������������������������������������������� 2
Installing with HomeBrew or MacPorts����������������������������������������������������������������������������������� 4
Installing from a Tar File���������������������������������������������������������������������������������������������������������� 5
Installing Without sudo������������������������������������������������������������������������������������������������������������ 6
Installing from Source Code���������������������������������������������������������������������������������������������������� 6
Multiversion Setup with NVM�������������������������������������������������������������������������������������������������� 7
Multiversion Setup with NVM for Windows����������������������������������������������������������������������������� 8
Alternative Multiversion Systems�������������������������������������������������������������������������������������������� 8
Updating npm�������������������������������������������������������������������������������������������������������������������������� 9
Checking the Installation��������������������������������������������������������������������������������������������������������� 9
Node.js Console (REPL)���������������������������������������������������������������������������������������������������������� 10
Launching Node.js Scripts���������������������������������������������������������������������������������������������������������� 11
Node.js Basics and Syntax���������������������������������������������������������������������������������������������������������� 12
Loose Typing�������������������������������������������������������������������������������������������������������������������������� 12
Buffer—Node.js Super Data Type������������������������������������������������������������������������������������������ 13
Object Literal Notation����������������������������������������������������������������������������������������������������������� 14
Functions������������������������������������������������������������������������������������������������������������������������������� 15
Arrays������������������������������������������������������������������������������������������������������������������������������������ 18
Prototypal Nature������������������������������������������������������������������������������������������������������������������� 19
Conventions��������������������������������������������������������������������������������������������������������������������������� 21

v
Table of Contents

Node.js Globals and Reserved Keywords������������������������������������������������������������������������������ 24


__dirname vs. process.cwd�������������������������������������������������������������������������������������������������� 29
Browser Application Programming Interface Helpers����������������������������������������������������������� 29
Node.js Core Modules������������������������������������������������������������������������������������������������������������ 31
Handy Node.js Utilities����������������������������������������������������������������������������������������������������������� 33
Reading to and Writing from the File System in Node.js������������������������������������������������������� 34
Streaming Data in Node.js����������������������������������������������������������������������������������������������������� 35
Installing Node.js Modules with npm������������������������������������������������������������������������������������ 35
Taming Callbacks in Node.js�������������������������������������������������������������������������������������������������� 36
Hello World Server with HTTP Node.js Module���������������������������������������������������������������������� 37
Debugging Node.js Programs����������������������������������������������������������������������������������������������������� 38
Core Node.js Debugger���������������������������������������������������������������������������������������������������������� 39
Debugging with Node Inspector�������������������������������������������������������������������������������������������� 40
Node.js IDEs and Code Editors���������������������������������������������������������������������������������������������������� 45
Watching for File Changes���������������������������������������������������������������������������������������������������������� 49
Summary������������������������������������������������������������������������������������������������������������������������������������ 50

Chapter 2: Using Express.js to Create Node.js Web Apps��������������������������������������� 51


What Is Express.js?��������������������������������������������������������������������������������������������������������������������� 52
How Express.js Works����������������������������������������������������������������������������������������������������������������� 56
Express.js Installation����������������������������������������������������������������������������������������������������������������� 57
Express.js Generator Version������������������������������������������������������������������������������������������������� 57
Express.js Generator Installation������������������������������������������������������������������������������������������� 58
Local Express.js��������������������������������������������������������������������������������������������������������������������� 59
Express.js Scaffolding����������������������������������������������������������������������������������������������������������������� 61
Express.js Command-Line Interface�������������������������������������������������������������������������������������� 63
Routes in Express.js�������������������������������������������������������������������������������������������������������������� 65
Middleware as the Backbone of Express.js��������������������������������������������������������������������������� 68
Configuring an Express.js App����������������������������������������������������������������������������������������������� 69
Pug Is Haml for Express.js/Node.js���������������������������������������������������������������������������������������� 69
Final Thoughts Scaffolding���������������������������������������������������������������������������������������������������� 70

vi
Table of Contents

The Blog Project Overview���������������������������������������������������������������������������������������������������������� 70


Submitting the Data��������������������������������������������������������������������������������������������������������������� 72
Express.js Hello World Example�������������������������������������������������������������������������������������������������� 76
Setting Up Folders����������������������������������������������������������������������������������������������������������������� 76
npm init and package.json���������������������������������������������������������������������������������������������������� 77
Dependency Declaration: npm install������������������������������������������������������������������������������������ 79
The App.js File����������������������������������������������������������������������������������������������������������������������� 80
Meet Pug: One Template to Rule Them All����������������������������������������������������������������������������� 85
Running the Hello World App������������������������������������������������������������������������������������������������� 86
Summary������������������������������������������������������������������������������������������������������������������������������������ 87

Chapter 3: TDD and BDD for Node.js with Mocha��������������������������������������������������� 89


Installing and Understanding Mocha������������������������������������������������������������������������������������������ 90
Understanding Mocha Hooks������������������������������������������������������������������������������������������������������ 94
TDD with the Assert�������������������������������������������������������������������������������������������������������������������� 96
Chai Assert���������������������������������������������������������������������������������������������������������������������������� 99
BDD with Expect����������������������������������������������������������������������������������������������������������������������� 101
Expect Syntax���������������������������������������������������������������������������������������������������������������������� 103
Project: Writing the First BDD Test for Blog������������������������������������������������������������������������������� 104
Putting Configs into a Makefile������������������������������������������������������������������������������������������� 108
Summary���������������������������������������������������������������������������������������������������������������������������������� 111

Chapter 4: Template Engines: Pug and Handlebars���������������������������������������������� 113


Pug Syntax and Features���������������������������������������������������������������������������������������������������������� 114
Tags������������������������������������������������������������������������������������������������������������������������������������� 114
Variables/Locals������������������������������������������������������������������������������������������������������������������ 117
Attributes����������������������������������������������������������������������������������������������������������������������������� 118
Literals��������������������������������������������������������������������������������������������������������������������������������� 120
Text�������������������������������������������������������������������������������������������������������������������������������������� 121
Script and Style Blocks������������������������������������������������������������������������������������������������������� 122
JavaScript Code������������������������������������������������������������������������������������������������������������������� 122
Comments��������������������������������������������������������������������������������������������������������������������������� 123

vii
Table of Contents

Conditions (if)���������������������������������������������������������������������������������������������������������������������� 124


Iterations (each loops)��������������������������������������������������������������������������������������������������������� 124
Filters���������������������������������������������������������������������������������������������������������������������������������� 125
Interpolation������������������������������������������������������������������������������������������������������������������������ 126
Case������������������������������������������������������������������������������������������������������������������������������������� 126
Mixins���������������������������������������������������������������������������������������������������������������������������������� 127
Include��������������������������������������������������������������������������������������������������������������������������������� 128
Extend���������������������������������������������������������������������������������������������������������������������������������� 129
Standalone Pug Usage�������������������������������������������������������������������������������������������������������������� 129
Handlebars Syntax�������������������������������������������������������������������������������������������������������������������� 134
Variables������������������������������������������������������������������������������������������������������������������������������ 134
Iteration (each)�������������������������������������������������������������������������������������������������������������������� 135
Unescaped Output��������������������������������������������������������������������������������������������������������������� 136
Conditions (if)���������������������������������������������������������������������������������������������������������������������� 137
Unless���������������������������������������������������������������������������������������������������������������������������������� 138
With������������������������������������������������������������������������������������������������������������������������������������� 138
Comments��������������������������������������������������������������������������������������������������������������������������� 139
Custom Helpers������������������������������������������������������������������������������������������������������������������� 140
Includes (Partials)���������������������������������������������������������������������������������������������������������������� 142
Standalone Handlebars Usage�������������������������������������������������������������������������������������������������� 142
Pug and Handlebars Usage in Express.js���������������������������������������������������������������������������������� 146
Pug and Express.js�������������������������������������������������������������������������������������������������������������� 148
Handlebars and Express.js�������������������������������������������������������������������������������������������������� 149
Project: Adding Pug Templates to Blog������������������������������������������������������������������������������������� 150
layout.pug���������������������������������������������������������������������������������������������������������������������������� 150
index.pug����������������������������������������������������������������������������������������������������������������������������� 154
article.pug���������������������������������������������������������������������������������������������������������������������������� 156
login.pug������������������������������������������������������������������������������������������������������������������������������ 157
post.pug������������������������������������������������������������������������������������������������������������������������������� 158
admin.pug��������������������������������������������������������������������������������������������������������������������������� 160
Summary���������������������������������������������������������������������������������������������������������������������������������� 163

viii
Table of Contents

Chapter 5: Persistence with MongoDB and Mongoskin���������������������������������������� 165


Easy and Proper Installation of MongoDB��������������������������������������������������������������������������������� 166
How to Run the Mongo Server�������������������������������������������������������������������������������������������������� 168
Data Manipulation from the Mongo Console����������������������������������������������������������������������������� 170
MongoDB Console in Detail������������������������������������������������������������������������������������������������������� 172
Minimalistic Native MongoDB Driver for Node.js Example�������������������������������������������������������� 175
Main Mongoskin Methods��������������������������������������������������������������������������������������������������������� 182
Project: Storing Blog Data in MongoDB with Mongoskin���������������������������������������������������������� 186
Project: Adding MongoDB Seed Data����������������������������������������������������������������������������������� 186
Project: Writing Mocha Tests����������������������������������������������������������������������������������������������� 188
Project: Adding Persistence������������������������������������������������������������������������������������������������� 191
Running the App������������������������������������������������������������������������������������������������������������������������ 202
Summary���������������������������������������������������������������������������������������������������������������������������������� 204

Chapter 6: Security and Auth in Node.js��������������������������������������������������������������� 205


Authorization with Express.js Middleware�������������������������������������������������������������������������������� 206
Token-Based Authentication����������������������������������������������������������������������������������������������������� 207
JSON Web Token (JWT) Authentication������������������������������������������������������������������������������������� 209
Session-Based Authentication�������������������������������������������������������������������������������������������������� 216
Project: Adding E-mail and Password Login to Blog����������������������������������������������������������������� 218
Session Middleware������������������������������������������������������������������������������������������������������������ 219
Authorization in Blog����������������������������������������������������������������������������������������������������������� 220
Authentication in Blog��������������������������������������������������������������������������������������������������������� 224
Running the App������������������������������������������������������������������������������������������������������������������ 227
The oauth Module��������������������������������������������������������������������������������������������������������������������� 227
Twitter OAuth 2.0 Example with Node.js OAuth������������������������������������������������������������������� 228
Everyauth���������������������������������������������������������������������������������������������������������������������������� 230
Project: Adding Twitter OAuth 1.0 Sign-in to Blog with Everyauth�������������������������������������������� 231
Adding a Sign-in with a Twitter Link����������������������������������������������������������������������������������� 231
Configuring the Everyauth Twitter Strategy������������������������������������������������������������������������� 232
Summary���������������������������������������������������������������������������������������������������������������������������������� 236

ix
Table of Contents

Chapter 7: Boosting Node.js and MongoDB with Mongoose��������������������������������� 239


Mongoose Installation��������������������������������������������������������������������������������������������������������������� 240
DB Connection in a Standalone Mongoose Script��������������������������������������������������������������������� 240
Mongoose Schemas������������������������������������������������������������������������������������������������������������������ 244
Hooks for Keeping Code Organized������������������������������������������������������������������������������������������� 248
Custom Static and Instance Methods��������������������������������������������������������������������������������������� 248
Mongoose Models��������������������������������������������������������������������������������������������������������������������� 250
Relationships and Joins with Population���������������������������������������������������������������������������������� 253
Nested Documents�������������������������������������������������������������������������������������������������������������������� 257
Virtual Fields����������������������������������������������������������������������������������������������������������������������������� 259
Schema Type Behavior Amendment������������������������������������������������������������������������������������������ 261
Express.js + Mongoose = True MVC����������������������������������������������������������������������������������������� 263
Summary���������������������������������������������������������������������������������������������������������������������������������� 276

Chapter 8: Building Node.js REST API Servers with Express.js and Hapi������������ 277
RESTful API Basics�������������������������������������������������������������������������������������������������������������������� 279
Project Dependencies��������������������������������������������������������������������������������������������������������������� 281
Test Coverage with Mocha and Superagent������������������������������������������������������������������������������ 283
REST API Server Implementation with Express and Mongoskin����������������������������������������������� 289
Refactoring: Hapi REST API Server�������������������������������������������������������������������������������������������� 298
Summary���������������������������������������������������������������������������������������������������������������������������������� 305

Chapter 9: Real-Time Apps with WebSocket, Socket.IO, and DerbyJS����������������� 307


What Is WebSocket?����������������������������������������������������������������������������������������������������������������� 308
Native WebSocket and Node.js with the ws Module Example�������������������������������������������������� 309
Browser WebSocket Implementation���������������������������������������������������������������������������������� 309
Socket.IO and Express.js Example�������������������������������������������������������������������������������������������� 313
Collaborative Online Code Editor Example with DerbyJS, Express.js, and MongoDB���������������� 319
Project Dependencies and package.json����������������������������������������������������������������������������� 319
Server-side Code����������������������������������������������������������������������������������������������������������������������� 321
DerbyJS App������������������������������������������������������������������������������������������������������������������������ 323
Summary���������������������������������������������������������������������������������������������������������������������������������� 330
x
Table of Contents

Chapter 10: Getting Node.js Apps Production Ready�������������������������������������������� 331


Environment Variables�������������������������������������������������������������������������������������������������������������� 332
Express.js in Production������������������������������������������������������������������������������������������������������������ 332
Error Handling��������������������������������������������������������������������������������������������������������������������������� 335
Multithreading with Cluster������������������������������������������������������������������������������������������������������ 337
Multithreading with pm2����������������������������������������������������������������������������������������������������������� 340
Event Logging and Monitoring�������������������������������������������������������������������������������������������������� 341
Monitoring��������������������������������������������������������������������������������������������������������������������������� 342
Winston�������������������������������������������������������������������������������������������������������������������������������� 345
Building Tasks with Grunt��������������������������������������������������������������������������������������������������������� 346
A Brief on Webpack������������������������������������������������������������������������������������������������������������������� 350
Locking Dependencies�������������������������������������������������������������������������������������������������������������� 353
Git for Version Control and Deployments���������������������������������������������������������������������������������� 356
Installing Git������������������������������������������������������������������������������������������������������������������������� 356
Generating SSH Keys���������������������������������������������������������������������������������������������������������������� 357
Creating a Local Git Repository������������������������������������������������������������������������������������������� 360
Pushing the Local Repository to GitHub������������������������������������������������������������������������������ 360
Running Tests in Cloud with TravisCI���������������������������������������������������������������������������������������� 362
TravisCI Configuration���������������������������������������������������������������������������������������������������������� 363
Summary���������������������������������������������������������������������������������������������������������������������������������� 364

Chapter 11: Deploying Node.js Apps�������������������������������������������������������������������� 365


Deploying to Heroku������������������������������������������������������������������������������������������������������������������ 365
Deploying to Amazon Web Services������������������������������������������������������������������������������������������ 372
Keeping Node.js Apps Alive with forever, Upstart, and init.d����������������������������������������������������� 377
forever��������������������������������������������������������������������������������������������������������������������������������� 378
Upstart Scripts��������������������������������������������������������������������������������������������������������������������� 378
init.d������������������������������������������������������������������������������������������������������������������������������������ 381
Serving Static Resources Properly with Nginx������������������������������������������������������������������������� 383
Caching with Varnish���������������������������������������������������������������������������������������������������������������� 386
Summary���������������������������������������������������������������������������������������������������������������������������������� 388

xi
Table of Contents

Chapter 12: Modularizing Your Code and Publishing Node.js Modules to npm���� 389
Recommended Folder Structure����������������������������������������������������������������������������������������������� 391
Modularizing Patterns��������������������������������������������������������������������������������������������������������������� 392
Composing package.json���������������������������������������������������������������������������������������������������������� 395
Publishing to npm��������������������������������������������������������������������������������������������������������������������� 397
Not-Locking Versions���������������������������������������������������������������������������������������������������������������� 398
Summary���������������������������������������������������������������������������������������������������������������������������������� 399

Chapter 13: Node HTTP/2 Servers������������������������������������������������������������������������ 401


Brief Overview of HTTP/2���������������������������������������������������������������������������������������������������������� 401
SSL Key and Certificate������������������������������������������������������������������������������������������������������������ 404
HTTP/2 Node Server������������������������������������������������������������������������������������������������������������������ 407
Node HTTP/2 Server Push��������������������������������������������������������������������������������������������������������� 413
Summary���������������������������������������������������������������������������������������������������������������������������������� 416

Chapter 14: Asynchronous Code in Node������������������������������������������������������������� 417


async Module���������������������������������������������������������������������������������������������������������������������������� 418
Promises����������������������������������������������������������������������������������������������������������������������������������� 421
Async Functions������������������������������������������������������������������������������������������������������������������������ 426
Summary���������������������������������������������������������������������������������������������������������������������������������� 429

Chapter 15: Node Microservices with Docker and AWS ECS�������������������������������� 431
Installing Installations��������������������������������������������������������������������������������������������������������������� 432
Installing Docker Engine������������������������������������������������������������������������������������������������������ 432
Getting an AWS Account������������������������������������������������������������������������������������������������������ 434
Installing AWS CLI���������������������������������������������������������������������������������������������������������������� 436
Dockerizing Node Microservice������������������������������������������������������������������������������������������������ 437
Creating/Copying the Node Project������������������������������������������������������������������������������������� 437
Creating a Node.js Dockerfile���������������������������������������������������������������������������������������������� 439
Use Docker Networks for Multi-container Setup���������������������������������������������������������������������� 445
Creating a Docker Network������������������������������������������������������������������������������������������������� 445
Launch App into a Network������������������������������������������������������������������������������������������������� 446

xii
Table of Contents

Node Containers in AWS with EC2 ECS������������������������������������������������������������������������������������� 449


Creating a Registry (ECR)���������������������������������������������������������������������������������������������������� 450
Create a New Task Definition����������������������������������������������������������������������������������������������� 457
Creating Cluster������������������������������������������������������������������������������������������������������������������� 464
Creating the Cloud Container Service and Verifying it��������������������������������������������������������� 469
Terminate Service and Cluster/Instances���������������������������������������������������������������������������� 472
Summary���������������������������������������������������������������������������������������������������������������������������������� 472

Chapter 16: Serverless Node with AWS Lambda�������������������������������������������������� 473


Creating a DynamoDB Table������������������������������������������������������������������������������������������������������ 474
Creating an IAM Role to Access DynamoDB������������������������������������������������������������������������������ 475
Creating an AWS Lambda Resource������������������������������������������������������������������������������������������ 478
Creating an API Gateway Resource������������������������������������������������������������������������������������������� 484
Testing the RESTful API Microservice��������������������������������������������������������������������������������������� 489
Cleaning Up������������������������������������������������������������������������������������������������������������������������������� 493
Summary���������������������������������������������������������������������������������������������������������������������������������� 494

Chapter 17: Conclusion����������������������������������������������������������������������������������������� 495


Author Contact�������������������������������������������������������������������������������������������������������������������������� 496
Further Learning����������������������������������������������������������������������������������������������������������������������� 496

Index��������������������������������������������������������������������������������������������������������������������� 497

xiii
About the Author
Azat Mardan works in technology leadership at Indeed.com,
the world leader in job search. Azat is a JavaScript/Node.
js expert with dozens of published online courses on Node
University, edX, and Udemy, and books including much
praised top-sellers React Quickly (Manning, 2017), Full Stack
JavaScript (Apress, 2015), Practical Node.js (Apress, 2014),
Pro Express.js (Apress, 2014) and many others.
Two of Azat’s self-published books, Rapid Prototyping
with JS and Express.js Guide, became bestsellers on Amazon in
their categories before being revised and published by Apress.
In 2016 alone, Azat spoke at over a dozen tech conferences, including JSConf, Node
Summit, NDC, Node Interactive, ConFoo, ForwardJS, All Things Open, GDG DevFest,
Great Wide Open, and others. Over the years, Azat has shared a speaking platform with
prominent software gurus such as Douglas Crockford, Christian Heilmann, Jeff Atwood,
Dan Shaw, Mikeal Rogers, John Papa, Axel Rauschmayer, Kyle Simpso, Samer Buna,
James Halliday, Maxwell Ogden, Rey Bango, and many others.
Azat is an ex-Technology Fellow at Capital One, a top U.S. bank. At various times,
Azat has worked as software engineer and technology leader in different organizations,
including U.S. federal government agencies, Fortune 200 companies, small startups,
and medium-sized corporations. During his career, Azat has worked on teams with
prominent tech people such as Raquel Vélez (first engineer at npm), Jim Jagielski
(founder of Apache Foundation), Bobby Calderwood (contributor to ClojureScript), and
Mitch Pirtle (co-founder of Joomla!).
Azat has taught in-person and face-to-face over a thousand software engineers at
prominent U.S. and global corporations including Cisco, Walmart, Starbucks, Michael
Kors, Salesforce, 20th Century Fox/Fox Studios, VMWare, CapitalOne, OnDeck,
Northwestern Mutual, HubSpot, UC Davis, The University of Arizona, Intuit, DocuSign,
Intuit, Macy’s, Twillio, The Orchard, and Apple.
In his spare time, Azat enjoys a cup of Americano with ghee while recording videos
for Node University (https://node.university), where thousands of developers
sharpen and master their Node skills.
xv
Acknowledgments
Thank you to the supporters of my Kickstarter campaign. Without you, I probably would
have not worked on this release so hard, and maybe wouldn’t have worked at all. You are
awesome because you made this new edition a reality. Not only that but you have made
this book available on GitHub for the entire world to read and learn Node, the greatest
technology for building web applications ever.
In particular, a great many thanks to individual Kickstarter supporters (who will be
getting the signed print edition books and other rewards): Matthew Amacker, Jordan
Horiuchi, Tim Chen, Alexey Bushnev, Aleksey Maksimov, Maurice van Cooten, Ryan, Ng
Yao Min, Kommana Karteek, Elias Yousef, Arhuman, Javier Armendariz, Dave Anderson,
and Edithson Abelard. You guys are brilliant!
I can’t not mention the biggest supporter, DevelopIntelligence, which is one of the
best training companies, if not the best (www.developintelligence.com). If you need
to train your software engineers in anything, e-mail them. Seriously, DevelopIntelligence
has been around for more than 20 years and has great teachers and great technical
classes. I was one of their instructors, so I know.
I convey my gratitude to all the wonderful people I’ve encountered in my software
engineering career. These people supported, mentored, and trusted me with new
challenges, helped me to find mistakes, and pushed my limits.
Of course, this book wouldn’t be possible without the assistance, research, and
championing done by my wonderful Apress editors. I especially thank Louise Corrigan,
James Markham, Cat Ohala, and Peter Elst.
Also, appreciation and many thanks go to the readers who kindly provided feedback
to the first edition of Practical Node.js, my Webapplog.com (http://webapplog.com)
blog posts, and my prior books.

xvii
Introduction
More and more books and online resources are being published that cover Node.js
basics (typically, how-to’s of Hello World and simple apps). For the most part, these
tutorials rely on core modules only or maybe one or two npm packages. This “sandbox”
approach of tutorials is easy and doesn’t require many dependencies, but it couldn’t be
further from the actual Node.js stack.
This is especially true with Node.js, the core of which—by design—is kept lean and
minimal. At the same time, the vast “userland” (that is, npm) provides an ecosystem
of packages/modules to serve specific granular purposes. Therefore, there is a need
to show how Node.js is used in the industry and to have it all in one place—the all-­
encompassing practical resource that can be used as a learning tool, code cookbook, and
reference.

What This Book Is and What It’s Not


Practical Node.js: Building Real-World Scalable Web Apps is a hands-on manual for
developing production-ready web applications and services by leveraging the rich
ecosystem of Node.js packages. This is important because real applications require
many components, such as security, deployment, code organization, database drivers,
template engines, and more. That’s why I include extensive 12-chapter coverage of third-­
party services, command-line tools, npm modules, frameworks, and libraries.
Just to give you some idea, Practical Node.js is a one-stop place for getting started
with Express.js 4, Hapi.js, DerbyJS, Mongoskin, Mongoose, Everyauth, Mocha, Jade,
Socket.IO, TravisCI, Heroku, Amazon Web Services (AWS), and many other technologies.
Most of these are vital for any serious project.
In this book we’ll create a few projects by building, step by step, a straightforward
concept into a more complicated application. These projects can also serve as a
boilerplate for jump-starting your own development efforts. The examples also show
industry best practices to help you avoid costly mistakes. Last but not least, many topics
and chapters serve as a reference to which you can always return later when you’re faced
with a challenging problem.

xix
Introduction

Practical Node.js aims to save you time and make you a more productive Node.js
programmer. Although the first chapter is dedicated to installations and a few important
differences between Node.js and browser JavaScript, I didn’t want to dilute the core
message of making production-ready apps, or make the book even larger and more
convoluted. Therefore, this book is not a beginner’s guide, and there is no extensive
immersion into the inner workings of the Node.js platform and its core modules.
I also can’t guarantee that I’ve explained each component and topic to the extent
you need, because the nature of your project might be very specific. Most chapters in the
book help you to get started with the stack. There is simply no realistic way to fit so many
topics in one book and cover them comprehensively.
Another caveat of this book (and virtually any programming book) is that the
versions of the packages we use will eventually become obsolete. Often, this isn’t an
issue because, in this book, versions are stated and locked explicitly. So, no matter what,
the examples will continue to work with book’s versions. Even if you decide to use the
latest versions, in many cases that might not be an issue because essentials remain the
same. However, if you go this off-path route, once in a while you might be faced with a
breaking change introduced by the latest versions.

Who Can Benefit from This Book


Practical Node.js is an intermediate- to advanced-level book on programming with
Node.js. Consequently, to get the most out of it, you need to have prior programming
experience and some exposure to Node.js. I assume readers’ prior knowledge of
computer science, programming concepts, web development, Node.js core modules,
and the inner workings of HTTP and the Internet.
However, depending on your programming level and ability to learn, you can fill
in any knowledge gaps very quickly by visiting links to official online documentation
and reading external resources referenced in this book. Also, if you have a strong
programming background in some other programming language, it should be relatively
easy for you to start Node.js development with Practical Node.js.
Written as it was for for intermediate and advanced software engineers, there are
categories of programmers who can most benefit from it:

1. Generalist or full-stack developers including development


operation (DevOps) and quality assurance (QA) automation
engineers
xx
Introduction

2. Experienced front-end web developers with a strong background


and understanding of browser JavaScript

3. Skilled back-end software engineers coming from other languages,


such as Java, PHP, and Ruby, who don’t mind doing some extra
work getting up to speed with JavaScript.

What You’ll Learn


Practical Node.js takes you from an overview of JavaScript and Node.js basics, through
installing all the necessary modules, to writing and deploying web applications, and
everything in between. It covers libraries including but not limited to Express.js 4 and
Hapi.js frameworks, Mongoskin and the Mongoose object-relational mapping (ORM)
library for the MongoDB database, Jade and Handlebars template engines, Auth and
Everyauth libraries for OAuth integrations, the Mocha testing framework and Expect
test-driven development/behavior-driven development language, and the Socket.IO and
DerbyJS libraries for WebSocket real-time communication.
In the deployment chapters (Chapters 10 and 11), the book covers how to use Git
and deploy to Heroku, and it provides examples of how to deploy to AWS, daemonize
apps, and use NGINX, Varnish Cache, Upstart, init.d, and the forever module.
The hands-on approach of this book walks you through iterating on the Blog project
we’ll be building, in addition to many other smaller examples. You’ll build database
scripts, representational state transfer (RESTful) application programming interfaces
(APIs), tests, and full-stack apps—all from scratch. You’ll also discover how to write your
own Node.js modules and publish them on npm.
Practical Node.js will show you how to do the following:

• Build web apps with Express.js 4, MongoDB, and the Jade template
engine

• Use various features of Jade and Handlebars

• Manipulate data from the MongoDB console

• Use the Mongoskin and Mongoose ORM libraries for MongoDB

• Build REST API servers with Express.js 4 and Hapi.js

• Test Node.js web services with Mocha, Expect, and TravisCI

xxi
Introduction

• Use token and session-based authentication

• Implement a third-party (Twitter) OAuth strategy with Everyauth

• Build WebSocket apps using Socket.IO and DerbyJS libraries

• Prepare code for production with Redis, Node.js domains, and the
cluster library using tips and best practices

• Deploy apps to Heroku using Git

• Install necessary Node.js components on an AWS instance

• Configure NGINX, Upstart, Varnish, and other tools on an AWS


instance

• Write your own Node.js module and publish it on npm

You already know what Node.js is. It’s time to learn what you can do with it and see
how far you can take it.

Why You Should Read This Book


Practical Node.js was designed to be one stop for going from Hello World examples to
building apps in a professional manner. You get a taste of the most widely used Node.js
libraries in one place, along with best practices and recommendations based on years
of building and running Node.js apps in production. The libraries covered in the book
greatly enhance the quality of code and make you more productive. Also, although the
material in this book isn’t groundbreaking, the convenience of the format will save you
hours of frustration researching on the Internet. Practical Node.js is here to help you to
jump-start your Node.js development.

N
 otation
The book and all its source code follow StandardJS (https://standardjs.com) coding
style. When it comes to showing the code in the book, this book follows a few formatting
conventions. Code is in monospace font. This is inline code, var book = {name:
'Practical Node.js'};, and this is a code listing:

xxii
Introduction

server.on('stream', (stream, headers) => {


  // Stream is a Duplex
  stream.respond({
    'content-type': 'text/html',
    ':status': 200
  })
  stream.end('<h1>Hello World<h1>')
})

Unfortunately, book pages are narrower than expandable code editor windows.
That’s why some code formatting in books may be slightly different than StandardJS,
because by necessity sometimes there are more line breaks.
For this reason, be especially careful in the code listings about maintaining proper
syntax, avoiding typos, and not having extra line breaks. If in doubt, always refer to the
GitHub source code instead of relying on the book because the GitHub source code will
always have more proper formatting (StandardJS) and may even contain a bug fix that
somehow sneaked into a code listing in the book.
If the code begins with $, that means it’s meant to be executed in the terminal/
command line. However, if the code line starts with >, the code is meant for the virtual
environment (the console—either Node.js or MongoDB). If the Node.js module name
is in code font, that means it’s the npm name and you can use it with npm and the
require() method, such as superagent .

S
 ource Code
Learning is more effective when we apply our knowledge right away. For this reason,
virtually every chapter in Practical Node.js ends with a hands-on exercise. For your
convenience, and because the publisher and I believe in open source and transparency,
all the book’s examples are available publicly (free of charge) for exploration and
execution on GitHub at https://github.com/azat-co/practicalnode.

xxiii
Introduction

E rrata and Contacts


If you spot any mistakes or typos (and I’m sure you will), please open an issue—or,
even better, make a pull request and fix it on the GitHub repository of the book’s
examples at https://github.com/azat-co/practicalnode. For all other updates
and contact information, the canonical home of Practical Node.js on the Internet is
­http://practicalnodebook.com.

xxiv
CHAPTER 1

Setting up Node.js
and Other Essentials
In many technologies, it’s vital to have the proper foundation set up first, before moving
on to solving more complex problems. With Node.js, proper foundation is even more
important because of all the bad syntax and quirks that JavaScript brings to Node. In this
chapter, we cover the following:

• Node.js and npm (Node package manager) installation

• Node.js script launches

• Node.js syntax and basics

• Node.js integrated development environments (IDEs) and


code editors

• Awareness of file changes

• Node.js program debugging

Installing Node.js and npm


Although your operating system (OS) might have Node.js installed on it already, you
should update to at least version 8.x, which is the latest recommended long-term
support (LTS) version as of this writing (July 2018). Version 8 is used in the examples and
projects of this book. Version 8 is LTS and the recommended choice because it will be
supported until October 2019 according to the Node official release schedule.
If you are reading the book after October 2019, please use the next LTS version for your
real-­life projects. You can still use Node.js version 8 for this book’s projects to ensure

1
© Azat Mardan 2018
A. Mardan, Practical Node.js, https://doi.org/10.1007/978-1-4842-3039-8_1
Chapter 1 Setting up Node.js and Other Essentials

smooth execution and lack of conflicts. In the following subsection, we examine a few
different approaches to installing Node.js:

• One-click installers: Probably the easiest and fastest way to get started
with the platform

• Installing with HomeBrew or MacPorts: Straightforward installation


for macOS users

• Installing from a tar file: An alternative installation from an


archive file

• Installing without sudo: The best way to avoid needing sudo


(admin rights) when using the node and npm commands
• Installing from a Git repo: An option for advanced developers who
need the latest version and/or contribute to the project

• Multiversion setup with Nave: a must-have for developers


contributing to projects that use different Node.js versions

• Multiversion setup with Node Version Manager (NVM): alternative to


Nave (see previous entry)

A note about Long-Term Support (LTS) and non-LTS versions: LTS versions have
longer maintenance window. This means that LTS versions will have patches and
updates longer than non-LTS versions. Thus LTS versions are recommended for most
users and production deployment (not because non- LTS is not proven but simply
because LTS has a longer support time).
LTS versions are even numbers, such as 4, 6, 8, 10, and so on. And non-LTS versions
are odd numbers. Non-LTS versions have the latest features before they are rolled out
to the next LTS version. We will be using LTS version 8.x. For more information and the
current release schedule, please see https://github.com/nodejs/LTS.

O
 ne-Click Installers
First, let’s go to http://nodejs.org and download a one-click installer for your OS
(Figure 1-1) by clicking on the Install button. Don’t choose binaries or source code
unless you know what to do with them or your OS is not present there (i.e., not Windows
or Mac).

2
Chapter 1 Setting up Node.js and Other Essentials

Figure 1-1. One-click installers for Node.js

The installers come with npm, Node package manager, which is an important tool
for managing dependencies.
If there’s no installer for your OS on the download page (page https://nodejs.
org/en/download), you can get the source code and compile it yourself (Figure 1-2).

3
Chapter 1 Setting up Node.js and Other Essentials

Figure 1-2. Multiple options for downloading

Note For older macOS machines, you can pick 32-bit versions.

Installing with HomeBrew or MacPorts


If you already have HomeBrew (brew) installed, first update the brew itself, and run
install commands:

$ brew update
$ brew install node

To install the latest Node version, run:

$ brew upgrade node

4
Chapter 1 Setting up Node.js and Other Essentials

If your macOS does not have HomeBrew, go to http://brew.sh or install it with the
following command:

$ ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/


install)"

Similarly, for MacPorts, run:

$ sudo port install nodejs

Installing from a Tar File


To install from a tar file (which is type of archive), set up a folder for the latest Node.js as
follows:

$ echo 'export PATH=$HOME/local/bin:$PATH' >> ~/.bashrc


$ . ~/.bashrc
$ mkdir ~/local
$ mkdir ~/node-latest-install
$ cd ~/node-latest-install

Note Advanced users who choose to make their own Node.js builds need to have
certain compilers installed first. For more information about building Node from
source, refer to the official documentation (https://github.com/nodejs/
node/blob/master/BUILDING.md).

Download the tar file with CURL and unpack it:

$ curl http://nodejs.org/dist/node-latest.tar.gz | tar xz --strip-­


components=1
$ ./configure --prefix=~/local

Build Node.js and install it:

$ make install
$ curl https://npmjs.org/install.sh | sh

5
Chapter 1 Setting up Node.js and Other Essentials

Tip If you find yourself getting errors when trying to install the module globally
via npm ($ npm install -g <packagename>), reinstall Node.js and npm
with the “Installing Without sudo” solution —discussed in the next section—to
eliminate the need to use sudo with the installation command.

Installing Without sudo


Sometimes, depending on your configuration, npm asks users for sudo —root user
permissions. To avoid using sudo, advanced developers can use the following:

$ sudo mkdir -p /usr/local/{share/man,bin,lib/node,include/node}


$ sudo chown -R $USER /usr/local/{share/man,bin,lib/node,
include/node}

Note Please be sure you are comfortable with the functionality of the chown
command before you run it.

Then, proceed with a normal installation:

$ mkdir node-install
$ curl http://nodejs.org/dist/node-v0.4.3.tar.gz | tar -xzf - -C
node-­install
$ cd node-install/*
$ ./configure
$ make install
$ curl https://npmjs.org/install.sh | sh

Installing from Source Code


If you want to use the latest core Node.js code, and maybe even contribute to the Node.
js and npm projects, your best choice is to use the installation from the source code that
is in Node repository on GitHub. This will allow you to change the Node code itself, and
then compile and run it.

6
Chapter 1 Setting up Node.js and Other Essentials

This step requires Git. To install it, go to http://git-scm.com and click Download.
For basic Git commands, refer to Chapter 11, which explores deployment.
For full, detailed instructions, go to https://github.com/nodejs/node/blob/
master/BUILDING.md. Here is the short version of the instructions:

1. Make the folders and add the path:

$ mkdir ~/local
$ echo 'export PATH=$HOME/local/bin:$PATH' >> ~/.bashrc
$ . ~/.bashrc

To clone the original Node.js repo from nodejs/node


(alternatively, you can fork it and clone your own repository), do
the following:

$ git clone git@github.com:nodejs/node.git


$ cd node
$ ./configure --prefix=~/local

2. Build Node with the make command:

$ make install

3. Repeat for npm:

$ git clone https://github.com/npm/npm


$ cd npm
$ make install

Multiversion Setup with NVM


If you plan to work on various Node projects, you might have to switch between multiple
versions of Node.js. To make things easier, I recommend using a version manager that
will allow you to install multiple versions and switch between them quickly and without
a hassle.
One of the most trusted and battle-tested version managers is nvm (Node Version
Manager): https://github.com/creationix/nvm. Install NVM as follows:

$ curl https://raw.github.com/creationix/nvm/master/install.sh | sh

7
Chapter 1 Setting up Node.js and Other Essentials

or

$ wget -qO- https://raw.github.com/creationix/nvm/master/


install.sh | sh

Then you should be ready to start using NVM and its install. For example, to
install Node v0.10, use this magic formula:

$ nvm install 0.10

After installing Node v0.10, to switch to the 0.10 version, apply the use command.
For example:

$ nvm use 0.10

NVM won’t move global npm modules from one version to another. If you are
switching from y to x, then use nvm install x --reinstall-packages-from=y to
reinstall all the global packages from “y” in the new “x”. For example, to move packages
to Node v8 from Node v6, use nvm install 8.4.0 --reinstall-packages-
from=6.11.2.

Multiversion Setup with NVM for Windows


Node Version Manager (nvm) for Windows is a separate project from original nvm which
is for macOS and Linux. nvm for Windows is (ironically) written in Go.
To download nvm for Windows, simply go to https://github.com/coreybutler/
nvm-windows releases and select the latest version of the installer.

Alternative Multiversion Systems


The most popular and used alternatives to NVM include the following tools:

• n (https://github.com/visionmedia/n): The original and simple


Node version manager without subshells (I still use it today on my
personal computers)

• nave (https://github.com/isaacs/nave): The version manager


written by the creator of npm Isaac Schelueter and that supports
subshells

• ndevn (https://github.com/riywo/ndenv): Node.js version


manager based on rbenv
8
Other documents randomly have
different content
The Project Gutenberg eBook of English-Bisaya
Grammar, in Twenty Eight Lessons
This ebook is for the use of anyone anywhere in the United
States and most other parts of the world at no cost and with
almost no restrictions whatsoever. You may copy it, give it away
or re-use it under the terms of the Project Gutenberg License
included with this ebook or online at www.gutenberg.org. If you
are not located in the United States, you will have to check the
laws of the country where you are located before using this
eBook.

Title: English-Bisaya Grammar, in Twenty Eight Lessons

Editor: Pedro Jiménez

Release date: February 5, 2013 [eBook #42026]


Most recently updated: October 23, 2024

Language: English

Credits: Produced by Jeroen Hellingman and the Online


Distributed
Proofreading Team at http://www.pgdp.net/ for Project
Gutenberg.

*** START OF THE PROJECT GUTENBERG EBOOK ENGLISH-BISAYA


GRAMMAR, IN TWENTY EIGHT LESSONS ***
English-Bisaya Grammar
In Twenty Eight Lessons.

Abridged from the Grammars


Of the Recoleto
Fathers, Guillen, Nicolas and Zueco
And translated into English for the use of the
American people
By
R. P. Fr. Pedro Jimenez
Recoleto.
Cebú—1904
Imprenta de «El Pais»
Nos Don Severino Piczon y Quinto,

PRESBITERO PROVISOR VICARIO GENERAL Y GOBERNADOR


ECLESIASTICO DEL OBISPADO DE CEBU S.V.

Por el tenor de las presentes y por lo que á nos toca, damos licencia para
que pueda imprimirse y publicarse la Gramática Anglo-Visaya escrita por el
R.P.Fr. Pedro Gimenez, Recoleto, atento á que según lo manifestado por el
M.R. Vicario Provincial de la órden, ha sido censurada por dos Religiosos
de la misma, que la han juzgado digna de que se dé á la estampa, no
conteniendo cosa alguna contraria á la fé y buenas costumbres, y mandamos
que se inserte este nuestro permiso al principio de cada ejemplar.

Dadas en Cebú, firmadas de nuestra mano, selladas con el de nuestro oficio


y refrendadas por el infrascrito Secretario de Gobierno á treinta de Enero de
mil novecientos cuatro.

SEVERINO PICZON.

(Hay un sello) (Hay una rúbrica)

Por mandado de SS. el Sr. Gobernador Eclesiástico.

JUAN P. GORORDO
(Hay una rúbrica.)
Preface

In presenting this English-Bisaya Grammar I do not pretend to be


considered an author, my only aspiration is to be useful to my American
brethren in the priesthood, in order that they in turn, may be so to the
Bisaya people. The priest for the Bisaya people must be one who will
devote all his attention to them, live among them, study their ways, their
character, their tendencies, and therefore, the study of their dialect is
absolutely necessary to him, since they, for the most part, do not know how
to speak either Spanish or English. In preparing this compilation I have
used every effort to do it as well as possible, but I am only a pupil in both
the English and Bisaya languages, and I believe, the work is not as perfect
as would be desired, but I indulge a hope that the kindness of my readers
will excuse my faults.
Bisaya Alphabet.

The letters made use of in the Bisaya alphabet, are twenty in number as
follows:

A. Ah. N. Ai-nay.
B. Bay. Ng. Ai-ngay.
C. Thay. Ñ Ai-nyay.
D. Day. O. Oh.
E. A or ay. P. Pay.
G. Hay. Q. Coo.
H. Atchay. S. Es say.
I. EE. T. Tay.
L. Ai-lay. U. OO.
M. Ai-may. Y. EE (griega).

The vowels are A, E, I, O, U, and Y at the end of a word. The vowels are
never silent, except U in the syllables que, qui, the sound of which
corresponds to that heard in the English words Kedge, Keep, Key.

A sounds always like A in alam.


B sounds like B in back
C before a, o, u, sounds like K in English, as—caadlaoon—The dawn of
the day—Coco—Nail of the fingers.
D at the beginning of a word or in the middle, if preceded by a consonant,
is pronounced like in English. At the end of a word or between two
vowels has a sound between D and R, which may be obtained by
placing the tip of the tongue against the higher teeth turning the thick
part towards the roof of the mouth.
G has always a very smooth sound like in English before a, o, u, as—
ginicanan, forefathers—gintoon-an, scholar.
H has a slight aspirated sound like a very faintly aspirated h in English in
the words horse, hog—as—hocom, judge—habagat, a strong wind—
hilanat, fever.
E, these vowels although sound like in English, nevertheless, natives
I confound them very often: the same shall be said of the vowels O and
U; and this is the reason why the P. John Felix's Dictionary employs but
I and O, instead E, I—O, U.
L sounds like in English, as—lamdag, brightness—libac, backbiting.
M sounds like in English: as—mata, eye—motó-top.
N sounds like in English; as—nipis, fine, thin.
Ng this letter has no equivalent in English, and it must be heard from the
natives.
Ñ this letter has a strong nasal sound resembling that of n in the English
word “poniard” out of Bohol province, where it is pronounced as in the
English word—manger and written ny: as, caninyo, bonyag, instead of
caniño, boñag.
O sounds like in English; as—olan, rain—úhao, thirst.
P sounds as in English:—pito, seven—ponó, fill.
Q is always followed by u, and pronounced like K; as, quinabuhi, life,
quilay, eyebrow, quilquil, scratching.
S has always a harsh, hissing sound like ss in English. There is not a
word in Bisaya beginning with s followed by a consonant.
T sounds as in English, as—tabang, help, tiao, joke.
U sounds like in English in the words “proof, goose” but it is frequently
confounded with O. (See I and E on the preceding page).
Y sounds like ee in English at the end of a word; but before a vowel, or
between two vowels, sounds like in the English words “joke, jolt” as—
yabó, pour.—This letter when after a noun or pronoun, if the same
noun or pronoun, is employed instead of the particle ang, being as it
does, an article of appellative nouns. Examples: I did that—acó ang
nagbuhat niana, or, acoy nagbuhat niana—What is the reason of that.—
¿Onsa ba ang hingtungdan niana? or ¿Onsay hingtungdan niana?
First Lesson.
Of the article.
1.a The article in the Bisaya dialect is divided into determinate and
indeterminate and of the proper names.

2.a The determinate article is ang for singular, and ang mga or sa mga
according to the cases for plural.

3.a The indeterminate article is usa, one for the singular; and uban, pila or
mapila, some for the plural.

4.a The article of the proper names is si for both masculine and feminine.

Declension of the Articles.

DEFINITE ARTICLE.

SINGULAR.

N. The dog. Ang iro.


G. Of the dog. Sa iro.
D. To the dog. Sa iro.
Ac. The dog. Sa iro.
Vc. Oh dog. Sa iro.
Abl. With the dog. Sa iro.
PLURAL.

N. The dogs. Ang mga iro.


G. Of the dogs. Sa mga iro.
D. To the dogs. Sa mga iro.
Ac. The dogs. Sa mga iro.
Vc. Oh dogs. Sa mga iro.
Abl. With the dogs. Sa mga iro.

INDEFINITE ARTICLE.

SINGULAR

A letter. Usa ca sulat.


A dog. Usa ca iro.
One and another Ang usa ug ang usa

PLURAL.

Some wish, while others do not—ang uban bu-ut, ang uban dili—Some of
the trees, which are there—Pipila sa mga cahuy dihá.

The Article of the Proper Names


SINGULAR.

N. Peter. Si Pedro.
G. Of Peter. Ni Pedro.
D. To Peter. Can Pedro.
Ac. Peter. Can Pedro.
Vc. Peter. Oy Pedro.
Abl. With Peter. Can Pedro.

Remark l.a The article of the proper names is used also to point out a person
and his companions, as:

N. Joseph and his friends. Sila si José.


G. Of Joseph and of his friends. ila ni José.

2.a This article serves also to express kindness or love: as.—My Mother, Si
nanay.—My father, Si tatay.—The female child, Si inday—The parish
Priest, Si amoy—My eldest sister, Si manang.

3.a The English compounded words, wooden, golden etc. change the affix
en into nga, as:—The wooden cross, Ang cruz nga cahoy—The golden
ring, Ang singsing nga bulauan.

4.a When we point out the destination of a thing, it is employed the article
sa—Thus: The water vat, Ang tadyao sa tubig.—The bottle of wine, Ang
botella sa vino.

5.a When it refers to the property of any one, it is as follows.—John's hat,


Ang calo ni Juan.

6.a The article sa is employed, when the thing it refers to is determinate, but
when it is indeterminate, the article ug must be used, as: Bring the money,
Magdala ca sa sapi. Give me money, Taga-an mo acó ug salapi.

PLURAL.

1.a The plural is formed in Bisaya by placing mga after the article ang or
sa.

Declension.
N. The cats Ang mga iring
G. Of the cats Sa mga iring
D. To the cats Sa mga iring
Ac. The cats Sa mga iring
Vc. Oh cats Mga iring
Abl. With the cats Sa mga iring

2.a When the possessive case is placed before the name, it must be placed
between ang and mga, thus—My friends, ang acong mga higala—Your
shoes, ang imong mga sapin.

3.a Rem. The Bisaya article like the English, does not distinguish the
gender, but there are two ways of distinguishing the masculine and feminine
in this dialect: 1. By using different words: Ex, ang bana, the husband; ang
asaua, the wife; ang amahan, the father: ang inahan, the mother. 2. By the
suffixes lalaqui and babaye; Ex. ang iro nga lalaqui, the dog, ang iro nga
babaye, she dog; ang bata nga lalaqui, the boy; ang bata nga babaye, the
girl.

Examples of the article.

Joseph's soul, ang calag ni José—John's ground. ang yuta ni Juan, or ang
can Juan nga yuta—Who is at Peter's house, ang sa can Pedro nga balay, or
ang sa balay ni Pedro—Bring the rice: Magdala ca ug bugas—My mother
and sister are at John's cottage, Si nanay ug si inday tua sa camalig ni Juan
—My brothers are rich, Ang acong mga igso-on salapia-non man—Are you
Peter's father?, Icao ba ang amahan ni Pedro?—I am, Acó man—Who is the
owner? ¿Quinsa ba ang tagia?—Where is your son?, Hain ba ang imong
anac?—He is at the cockfight, Tua sia sa bulangan—Has he much money?,
Daghan ba ang iang salapi?—He has but a few coins, Pipila lamang ca
dacó—Let us go. Tala na quitá—Good by. Ari na came—That man is a
drunkard. Palahubóg man canang tao—He is a drinker, but not a drunkard.
Palainom man sia, apan dili palahubóg—Where is my father?, ¿Hain ba ang
acong amahan?—Here he is; Ania dinhi—Who are those men?, ¿Quinsa ba
canang mga tao?—They are my friends. Mao ang acong mga higala.

Exercise I.

Have you the bread?—Yes, sir, I have the bread: Have you your bread?—I
have my bread.—Have you the salt?—I have the salt—Have you my salt?
—I have your salt.—Have you the soap?—I have the soap—Which (onsa
nga) soap have you?—I have your soap—Which shirt have you?—I have
my shirt, (ang acong sinina)?—Have you much money?—I have much
money—Where is your sister?—She is at the garden (tanaman sa mga
bulac)—Where is your father?—He is here.
Second Lesson.
Of the nouns.
Supposing the pupil knows the classification of the nouns into proper,
common or appellative &., we shall occupy ourselves with their formation,
being as it is, so much diverse and usual.

A great number of nouns and verbs are compounded in Bisaya by means of


roots and particles.

The root is the word which contains in itself the signification of the thing,
but can not express it without any other word, which we call a particle, to
which the root must be united.

1.a With the particle ca at the beginning of the root, and an after, are formed
collective nouns, and nouns of place, as:—Grove, cacahoyan—Banana
plantation, casagingan.

2.a With the particle ca before, are formed the nouns of quality, as:—
Whiteness,—Ang caputi.

3.a By placing the particle pagca before the roots, are formed the abstract
nouns, and those pointing out the essence of the things, as:

Sweetness. Ang catam-is.


Mercy. Ang calo-oy.
Kindness. Ang caayo.
Divinity. Ang pagca Dios.
Humanity. Ang pagca taoo.
Hardness. Ang pagca guhi.

4.a With the particle isigca before the root, are formed correlative nouns,
placing the possessive pronoun in genitive case, as:
My like. Ang isigcataoo co.

5.a With the particles mag and man are formed substantive and adjective
nouns, duplicating the first syllable of the roots, thus:

The writer. Ang magsusulat.


The tailor. Ang magtatahi.
The maker. Ang magbubuhat.
The surgeon. Ang mananambal.
The almsgiver. Ang manlilimos.

6. With the particle pala before, are formed several substantives, as:

The drunk. Ang palahubóg.


The tippler Ang palainom.
The writer. Ang palasulat.

7.a With tag before the root, are formed nouns expressing the owner of a
thing, as:

The owner of the house. Ang tagbalay.


The master of the vessel. Ang tagsacayan.
The owner of the world. Ang tagcalibutan.

8.a Putting this same particle before the words signifying the seasons of the
year or the atmospherical changes, points out the time of these changes: and
placing it before words signifying the farming-works, it shows the time of
those operations. In some provinces are used also in this same sense, the
particles tig and tin, thus:

Rain time. tagolan.


Warm time. tiginit.
Harvest time. tagani—tinani, or tig-ani.
9.a The particle taga before the nouns of countries or nations, serves to ask
some one about his town, as:

Where are you from? Taga di-in ca ba?


I am from Spain. Taga España man acó.
Of which town? Taga di-in ca nga longsod?
From Cornago. Taga Cornago.

10.a Taga signifies also until, and points out the end of the action, as:

To the knee. Taga tohod.


To the neck. Taga liug.
As far as the floor. Taga salug.

11.a By means of the articles pag and pagca are formed the verbal
substantives. Ex:

Making or to make. Ang pagbuhat.


Walking or to walk. Ang paglacao.
Reading or to read. Ang pagbasa.
Resuscitating or to resuscitate. Ang pagcabanhao.
Dying or to die. Ang pagcamatay.

12.a Placing the particle tagi before the root it points out permanency on a
place, as:

Inhabitant of a place. Tagilongsod.


Countryman. Tagibanua.

Declension of the common nouns.


SINGULAR.

N. The cotton. ang gapas.


G. Of the cotton. sa gapas.
D. To the cotton. sa gapas.
PLURAL

N. The cottons. ang mga gapas.


G. Of the cottons. sa mga gapas.
D. To the cottons. sa mga gapas.

Practical examples

Don't approach the intoxicated man. Ayao icao dumo-ol sa palahubóg.


Hardness is the molave merit. Ang caayo sa tugás ana-a sa cagahi
nia.
Love your neighbour, for that is a Mahagugma ca sa imong isigcataoo,
commandment of God. cay gisugo sa Dios.
Don't sow nor plant in warm time. Sa tigadlao ayo pagtanom ug
pagpugás.
God is the Maker of all things. Ang Dios mao ang Magbubuhat sa
ngatanan.
The river water reaches as far as the Ang tubig sa subá miabut
waist. tagahaoac.
Who has my book? ¿Hain ba ang acong libro?
Where is my book? ¿Hain ba ang libro co?
I have it. Ania man canaco.
Where is the horse? ¿Hain ba ang cabayo?
I do not know. Ambut lamang.
How does that concern you? ¿Onsay imo dihá?
Every oneself. Iyahay lang quitá.
So must it be. Mao man cana unta.

Exercise II.

Good morning, how are you?—Very well, I thank you—Where are you
from?—I am from Spain—Of which town?—From Conago—Who (quinsa)
has my book?—I have it—Who is that young Lady?—She is Miss Kate—
Where is my trunk (caban)?—The servant has it—Have you my fine
glasses?—I have them—Have you the fine horses of my neighbours?—I
have not them—Who are you?—I am John—Are you Peter's father?—I am.
Third Lesson.
Of the cardinal numbers.
The father Encina divides the numbers into primitives, ordinals,
distributives and vicenales.

The primitive numerals are those which serve to count, and are the
followings:

1 One. Usá.
2 Two. Duha.
3 Three. Tolo.
4 Four. Upat.
5 Five. Lima.
6 Six. Unum.
7 Seven. Pito.
8 Eight. Ualo.
9 Nine. Siam.
10 Ten. Napolo.
11 Eleven. Napolo ug usá.
12 Twelve. Napolo ug duha.
13 Thirteen. Napolo ug tolo.
14 Fourteen. Napolo ug upat.
15 Fifteen. Napolo ug lima.
16 Sixteen. Napolo ug unum.
17 Seventeen. Napolo ug pito.
18 Eighteen. Napolo ug ualo.
19 Nineteen. Napolo ug siam.
20 Twenty. Caluha-an.
30 Thirty. Catlo-an.
40 Forty. Capat-an.
50 Fifty. Calim-an.
60 Sixty. Canum-an.
70 Seventy. Capito-an.
80 Eighty. Caualo-an.
90 Ninety. Casiam-an.
100 One hundred. Usa ca gatus.
101 One hundred and one Usa ca gatus ug usá.
200 Two hundred. Duha ca gatus.
300 Three hundred. Tolo ca gatus.
1000 One thousand. Usa ca libo.
1001 One thousand and one. Usa ca libo ug usá.
2000 Two thousand. Duha ca libo.

Ten children. Napolo ca bata.


Twenty horses. Caluha-an ca cabayo.
Two hundred and twenty guns. Duha ca gatus caluha an ug duha ca
fusil.
Five hundred and ninety one Lima ca gatus casiam-an ug usa ca
soldiers. soldalo.
One thousand men. Usa ca libo ca taoo.

Remarks:

1.a The English forms “a hundred, a thousand”, are rendered into Bisaya by
usa ca gatus, usa ca libo. Expressions like “eighteen hundred” must be
translated as:—one thousand eight hundred, ex: The year 1898.—Usa ca
libo ualo ca gatus casiaman ug usa.

2.a The unity begins by a consonant duplicates, the first syllable, when
points out any quantity. The denaries are formed by putting before unity the
particle ca and an after, as we have seen. Ca serves also to join the numbers
to the nouns, thus: Usa cataoo.—Napolo ca pisos, upat ca adlao.
Ordinal Numbers.

1st. Ang nahaona.


2d. Ang icaduha.
3d. Ang icatolo-tlo.
4th. Ang icaupat-pat.
5th. Ang icalima.
6th. Ang icaunum.
7th. Ang icapito.
8th. Ang icaualo.
9th. Ang icasiam.
10th. Ang icapolo.
11th. Ang icapolo ug usa.
12th. Ang icapolo ug duha.
13th. Ang icapolo ug tolo.
14th. Ang icapolo ug upat
15th. Ang icapolo ug lima.
20th. Ang icacaluhaan.
21th. Ang icacaluhaan ug usa.
30th. Ang icacatloan.
40th. Ang icacaupatan
50th. Ang icacalim-an
100th. Ang icausa ca gatus.

The month.—Ang bulan.


The day.—Ang adlao.
The week.—Ang semana.
What is the date to day.—¿Icapila quita caron?—To-day is the sixth of
March of the year 1901.—Sa icaunum ca adlao sa bulan sa Marzo sa usa ca
libo siam ca gatus ng usa ca tuig.

Days of the week

Monday. Lunes.
Tuesday. Martes.
Wednesday. Miercoles.
Thursday. Jueves.
Friday. Viernes.
Saturday. Sabado.
Sunday. Domingo.

Months of the year.

(The months and the days of the week are taken from the Spanish
language).

January. Enero.
February. Febrero.
March. Marzo.
April. Abril.
May. Mayo.
June. Junio.
July. Julio.
August. Agosto.
September. Setiembre.
October. Octubre.
November. Noviembre.
December. Diciembre.

A century. Usa ca siglo.


A year. Usa ca tuig.
A month. Usa ca bulan.
A week. Usa ca semana.
A day. Usa ca adlao.
An hour. Usa ca horas.
A minute. Usa ca minuto.
To day. Caron adlao.
Yesterday. Cahapon.
To-morrow. Ugma.
Next year. Tuig nga muabut.
Last year. Tuig nga miagui.
Day before yesterday. Cahapon sa usa ca adlao.
Day after to-morrow. Ugma damlag.
Three days ago. Canianhi.
Last week. Semana nga miagui.
At half past one. Sa á la una y media.
At a quarter past one. Sa á la una y cuarto.
At a quarter to one. Cuarto sa la una.

Partitive Numbers
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

textbookfull.com

You might also like