Practical Node.Js Building Real-World Scalable Web Apps Second Edition

Practical Node.Js Building Real-World Scalable Web Apps Second Edition

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 [email protected], 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 .............................................................................................................

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    20 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us