logo资料库

Express.in.Action.2016.4.pdf

第1页 / 共258页
第2页 / 共258页
第3页 / 共258页
第4页 / 共258页
第5页 / 共258页
第6页 / 共258页
第7页 / 共258页
第8页 / 共258页
资料共258页,剩余部分请下载后查看
Front cover
brief contents
contents
preface
acknowledgments
about this book
Roadmap
Code conventions
Getting the source code
Author Online
About the author
about the cover illustration
Part 1—Intro
1 What is Express?
1.1 What is this Node.js business?
1.2 What is Express?
1.2.1 The functionality in Node.js
1.2.2 What Express adds to Node.js
1.3 Express’s minimal philosophy
1.4 The core parts of Express
1.4.1 Middleware
1.4.2 Routing
1.4.3 Subapplications
1.4.4 Conveniences
1.5 The ecosystem surrounding Express
1.5.1 Express vs. other web application frameworks
1.5.2 What Express is used for
1.5.3 Third-party modules for Node.js and Express
1.6 The obligatory Hello World
1.7 Summary
2 The basics of Node.js
2.1 Installing Node
2.1.1 Running your first Node script
2.2 Using modules
2.2.1 Requiring built-in modules
2.2.2 Requiring third-party modules with package.json and npm
2.2.3 Defining your own modules
2.3 Node: an asynchronous world
2.4 Building a web server with Node: the http module
2.5 Summary
3 Foundations of Express
3.1 Middleware
3.1.1 Hello World with Express
3.1.2 How middleware works at a high level
3.1.3 Middleware code that’s passive
3.1.4 Middleware code that changes the request and response
3.1.5 Third-party middleware libraries
3.2 Routing
3.3 Extending request and response
3.4 Views
3.5 Example: putting it all together in a guestbook
3.5.1 Getting set up
3.5.2 The main app code
3.5.3 Creating the views
3.5.4 Start it up
3.6 Summary
Part 2—Core
4 Middleware
4.1 Middleware and the middleware stack
4.2 Example app: a static file server
4.2.1 Getting set up
4.2.2 Writing your first middleware function: the logger
4.2.3 The static file server middleware
4.2.4 404 handler middleware
4.2.5 Switching your logger to an open source one: Morgan
4.2.6 Switching to Express’s built-in static file middleware
4.3 Error-handling middleware
4.4 Other useful middleware
4.5 Summary
5 Routing
5.1 What is routing?
5.1.1 A simple routing example
5.2 The features of routing
5.2.1 Grabbing parameters to routes
5.2.2 Using regular expressions to match routes
5.2.3 Grabbing query arguments
5.3 Using routers to split up your app
5.4 Serving static files
5.4.1 Static files with middleware
5.4.2 Routing to static files
5.5 Using Express with HTTPS
5.6 Putting it all together: a simple routing demo
5.6.1 Setting up
5.6.2 The main app code
5.6.3 The two views
5.6.4 The application in action
5.7 Summary
6 Building APIs
6.1 A basic JSON API example
6.2 A simple Express-powered JSON API
6.3 Create, read, update, delete APIs
6.3.1 HTTP verbs (also known as HTTP methods)
6.3.2 CRUD applications with HTTP methods
6.4 API versioning
6.5 Setting HTTP status codes
6.5.1 Setting HTTP status codes
6.5.2 The 100 range
6.5.3 The 200 range
6.5.4 The 300 range
6.5.5 The 400 range
6.5.6 The 500 range
6.6 Summary
7 Views and templates: Pug and EJS
7.1 Express’s view features
7.1.1 A simple view rendering
7.1.2 A complicated view rendering
7.1.3 Making all view engines compatible with Express: Consolidate.js
7.2 Everything you need to know about EJS
7.2.1 The syntax of EJS
7.3 Everything you need to know about Pug
7.3.1 The syntax of Pug
7.3.2 Layouts in Pug
7.3.3 Mixins in Pug
7.4 Summary
Part 3—Express in Context
8 Persisting your data with MongoDB
8.1 Why MongoDB?
8.1.1 How Mongo works
8.1.2 For you SQL users out there
8.1.3 Setting up Mongo
8.2 Talking to Mongo from Node with Mongoose
8.2.1 Setting up your project
8.2.2 Creating a user model
8.2.3 Using your model
8.3 Authenticating users with Passport
8.3.1 Setting up Passport
8.4 Summary
9 Testing Express applications
9.1 What is testing and why is it important?
9.1.1 Test-driven development
9.1.2 Cardinal rule: when in doubt, test
9.2 Introducing the Mocha testing framework
9.2.1 How does Node.js testing work?
9.2.2 Setting up Mocha and the Chai assertion library
9.2.3 What happens when you run your tests
9.2.4 Writing your first test with Mocha and Chai
9.2.5 Adding more tests
9.2.6 More features of Mocha and Chai
9.3 Testing Express servers with SuperTest
9.3.1 Testing a simple API
9.3.2 Filling in the code for your first tests
9.3.3 Testing HTML responses
9.4 Summary
10 Security
10.1 The security mindset
10.2 Keeping your code as bug-free as possible
10.2.1 Enforcing good JavaScript with JSHint
10.2.2 Halting after errors happen in callbacks
10.2.3 Perilous parsing of query strings
10.3 Protecting your users
10.3.1 Using HTTPS
10.3.2 Preventing cross-site scripting attacks
10.3.3 Cross-site request forgery (CSRF) prevention
10.4 Keeping your dependencies safe
10.4.1 Auditing the code
10.4.2 Keeping your dependencies up to date
10.4.3 Check against the Node Security Project
10.5 Handling server crashes
10.6 Various little tricks
10.6.1 No Express here
10.6.2 Preventing clickjacking
10.6.3 Keeping Adobe products out of your site
10.6.4 Don’t let browsers infer the file type
10.7 Summary
11 Deployment: assets and Heroku
11.1 LESS, a more pleasant way to write CSS
11.1.1 Variables
11.1.2 Functions
11.1.3 Mixins
11.1.4 Nesting
11.1.5 Includes
11.1.6 Alternatives to LESS
11.2 Using Browserify to require modules in the browser
11.2.1 A simple Browserify example
11.3 Using Grunt to compile, minify, and more
11.3.1 Installing Grunt
11.3.2 Compiling LESS with Grunt
11.3.3 Using Browserify with Grunt
11.3.4 Minifying the JavaScript with Grunt
11.3.5 Using Grunt watch
11.3.6 Other helpful Grunt tasks
11.4 Using connect-assets to compile LESS and CoffeeScript
11.4.1 Getting everything installed
11.4.2 Setting up the middleware
11.4.3 Linking to assets from views
11.4.4 Concatenating scripts with directives
11.5 Deploying to Heroku
11.5.1 Getting Heroku set up
11.5.2 Making a Heroku-ready app
11.5.3 Deploying your first app
11.5.4 Running Grunt on Heroku
11.5.5 Making your server more crash resistant
11.6 Summary
12 Best practices
12.1 Simplicity
12.2 File structure pattern
12.3 Locking down dependency versions
12.3.1 The simple way: eschewing optimistic versioning
12.3.2 The thorough way: npm’s shrinkwrap command
12.3.3 Upgrading and adding dependencies
12.4 Localized dependencies
12.4.1 Invoking commands directly
12.4.2 Executing commands from npm scripts
12.5 Summary
Appendix—Other helpful modules
index
Symbls
Numerics
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Z
Back cover
M A N N I N G IN ACTION Writing, building, and testing Node.js applications Evan M. Hahn
Express in Action Licensed to
Licensed to
Express in Action Writing, building, and testing Node.js applications EVAN M. HAHN M A N N I N G SHELTER ISLAND Licensed to
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: orders@manning.com ©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. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Development editor: Dan Maharry Technical development editor: Deepak Vohra Copyeditor: Linda Recktenwald 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 Licensed to
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 Licensed to
Licensed to
contents xiii preface acknowledgments about this book about the cover illustration xix xiv xvi 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 Licensed to
分享到:
收藏