ebook img

Express in Action PDF

258 Pages·2016·4.759 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Express in Action

M A N N I N G IN ACTION Writing, building, and testing Node.js applications Evan M. Hahn Express in Action Express in Action Writing, building, and testing Node.js applications EVAN M. HAHN 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] ©2016 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. Manning Publications Co. Development editor: Dan Maharry 20 Baldwin Road Technical development editor: Deepak Vohra PO Box 761 Copyeditor: Linda Recktenwald Shelter Island, NY 11964 Proofreader: Elizabeth Martin Technical proofreader: Matthew Merkes Typesetter: Dennis Dalinnik Cover designer: Marija Tudor ISBN: 9781617292422 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – EBM – 21 20 19 18 17 16 brief contents PART 1 INTRO ..........................................................................1 1 ■ What is Express? 3 2 ■ The basics of Node.js 18 3 ■ Foundations of Express 31 PART 2 CORE..........................................................................51 4 ■ Middleware 53 5 ■ Routing 70 6 ■ Building APIs 87 7 ■ Views and templates: Pug and EJS 104 PART 3 EXPRESS IN CONTEXT................................................117 8 ■ Persisting your data with MongoDB 119 9 ■ Testing Express applications 146 10 ■ Security 172 11 ■ Deployment: assets and Heroku 193 12 ■ Best practices 218 v contents preface xiii acknowledgments xiv about this book xvi about the cover illustration xix PART 1 INTRO...............................................................1 1 What is Express? 3 1.1 What is this Node.js business? 4 1.2 What is Express? 6 The functionality in Node.js 6 ■ What Express adds to Node.js 8 1.3 Express’s minimal philosophy 9 1.4 The core parts of Express 9 Middleware 10 ■ Routing 11 ■ Subapplications 11 Conveniences 12 1.5 The ecosystem surrounding Express 12 Express vs. other web application frameworks 12 ■ What Express is used for 14 ■ Third-party modules for Node.js and Express 14 1.6 The obligatory Hello World 16 1.7 Summary 16 vii viii CONTENTS 2 The basics of Node.js 18 2.1 Installing Node 19 Running your first Node script 19 2.2 Using modules 20 Requiring built-in modules 20 ■ Requiring third-party modules with package.json and npm 21 ■ Defining your own modules 24 2.3 Node: an asynchronous world 25 2.4 Building a web server with Node: the http module 28 2.5 Summary 30 3 Foundations of Express 31 3.1 Middleware 32 Hello World with Express 33 ■ How middleware works at a high level 34 ■ Middleware code that’s passive 36 Middleware code that changes the request and response 37 Third-party middleware libraries 38 3.2 Routing 40 3.3 Extending request and response 41 3.4 Views 43 3.5 Example: putting it all together in a guestbook 44 Getting set up 45 ■ The main app code 45 Creating the views 46 ■ Start it up 48 3.6 Summary 49 PART 2 CORE..............................................................51 4 Middleware 53 4.1 Middleware and the middleware stack 54 4.2 Example app: a static file server 55 Getting set up 56 ■ Writing your first middleware function: the logger 58 ■ The static file server middleware 60 404 handler middleware 61 ■ Switching your logger to an open source one: Morgan 63 ■ Switching to Express’s built-in static file middleware 64 4.3 Error-handling middleware 65 4.4 Other useful middleware 68 4.5 Summary 69 CONTENTS ix 5 Routing 70 5.1 What is routing? 70 A simple routing example 71 5.2 The features of routing 71 Grabbing parameters to routes 72 ■ Using regular expressions to match routes 72 ■ Grabbing query arguments 74 5.3 Using routers to split up your app 74 5.4 Serving static files 76 Static files with middleware 76 ■ Routing to static files 78 5.5 Using Express with HTTPS 78 5.6 Putting it all together: a simple routing demo 80 Setting up 81 ■ The main app code 81 ■ The two views 83 The application in action 85 5.7 Summary 85 6 Building APIs 87 6.1 A basic JSON API example 88 6.2 A simple Express-powered JSON API 90 6.3 Create, read, update, delete APIs 92 HTTP verbs (also known as HTTP methods) 93 CRUD applications with HTTP methods 95 6.4 API versioning 96 6.5 Setting HTTP status codes 99 Setting HTTP status codes 100 ■ The 100 range 101 The 200 range 101 ■ The 300 range 102 ■ The 400 range 102 ■ The 500 range 103 6.6 Summary 103 7 Views and templates: Pug and EJS 104 7.1 Express’s view features 105 A simple view rendering 105 ■ A complicated view rendering 105 ■ Making all view engines compatible with Express: Consolidate.js 108 7.2 Everything you need to know about EJS 109 The syntax of EJS 109

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.