Server Express.js + Middleware Request app.use() URL: /users/123 express.Router Defines the way requests to certain URLs are handled Method: GET params: {_id:123} cookie-parser Encrypts and decrypts info about a user’s status on app express.json Translates request contents to validate or modify data passport Authenticates user login information during app use Response express-ejs- Status: 200 Renders web page layouts Cookie: _j0n.W3x1eR Content-type: text/html EJS Mongoose.js The EJS templating engine Mongoose is an object transforms dynamic data and document modeling (ODM) layouts to produce an HTML library that simplifies your inter- page in the server’s response. action between the Node.js server and MongoDB database. Get Programming with Node.js Get Programming with Node.js Jonathan Wexler Foreword by Kyle Simpson MANNING Shelter Island For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email: [email protected] ©2019 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. Development editor: Toni Arritola Technical development editor: John Guthrie Review editor: Aleksandar Dragosavljević Production editor: David Novak Copyeditor: Kathy Simpson Proofreader: Melody Dolab Senior technical proofreader: Srihari Sriharan Technical proofreader: German Frigerio Typesetter: Dottie Marsico Cover designer: Monica Kamsvaag Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 ISBN 9781617294747 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – SP – 24 23 22 21 20 19 To the ones who got me programming and to my parents (the two people I always know aren’t reading my book), with love. Contents Foreword xv Preface xvii Acknowledgments xix About this book xxi About the author xxix Unit 0 GETTING SET UP 1 Lesson 0 Setting up Node.js and the JavaScript engine 3 What you’re going to learn 3 Understanding Node.js 5 Why learn to develop in Node.js? 9 Preparing yourself for this book 10 Summary 11 Lesson 1 Configuring your environment 12 Installing Node.js 12 Installing a text editor 16 Setting up SCM and deployment tools 17 Working with the Node.js REPL in terminal 19 Summary 21 Lesson 2 Running a Node.js application 22 Creating a JavaScript file 23 Running your JavaScript file with Node.js 24 Running individual JavaScript commands 25 Summary 27 vii viii Contents Unit 1 GETTING STARTED WITH NODE.JS 29 Lesson 3 Creating a Node.js module 31 Running npm commands 33 Initializing a Node.js application 35 Summary 39 Lesson 4 Building a simple web server in Node.js 40 Understanding web servers 41 Initializing the application with npm 43 Coding the application 43 Running the application 47 Summary 48 Lesson 5 Handling incoming data 49 Reworking your server code 50 Analyzing request data 51 Adding routes to a web application 55 Summary 58 Lesson 6 Writing better routes and serving external files 59 Serving static files with the fs module 60 Serving assets 64 Moving your routes to another file 67 Summary 72 Lesson 7 Capstone: Creating your first web application 73 Initializing the application 74 Understanding application directory structure 75 Creating main.js and router.js 76 Creating views 79 Adding assets 80 Creating routes 81 Summary 83 Unit 2 EASIER WEB DEVELOPMENT WITH EXPRESS.JS 85 Lesson 8 Setting up an app with Express.js 87 Installing the Express.js package 88 Building your first Express.js application 90