eBook
Available on Compatible NOOK devices, the free NOOK App and in My Digital Library.
Related collections and offers
Overview
Express in Action is a carefully designed tutorial that teaches you how to build web applications using Node and Express.
Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications.
About the Technology
Express.js is a web application framework for Node.js. Express organizes your server-side JavaScript into testable, maintainable modules. It provides a powerful set of features to efficiently manage routes, requests, and views along with beautiful boilerplate for your web applications. Express helps you concentrate on what your application does instead of managing time-consuming technical details.
About the Book
Express in Action teaches you how to build web applications using Node and Express. It starts by introducing Node's powerful traits and shows you how they map to the features of Express. You'll explore key development techniques, meet the rich ecosystem of companion tools and libraries, and get a glimpse into its inner workings. By the end of the book, you'll be able to use Express to build a Node app and know how to test it, hook it up to a database, and automate the dev process.
What's Inside
- Simplify Node app setup with Express
- Testing Express applications
- Use Express for easy access to Node features
- Data storage with MongoDB
- Covers Express 4 and Express 5 alpha
About the Reader
To get the most out of this book, you'll need to know the basics of web application design and be proficient with JavaScript.
About the Author
Evan Hahn is an active member of the Node and Express community and contributes to many open source JavaScript projects.
Table of Contents
- PART 1 INTRO
- What is Express?
- The basics of Node.js
- Foundations of Express PART 2 CORE
- Middleware
- Routing
- Building APIs
- Views and templates: Pug and EJS PART 3 EXPRESS IN CONTEXT
- Persisting your data with MongoDB
- Testing Express applications
- Security
- Deployment: assets and Heroku
- Best practices
Product Details
ISBN-13: | 9781638353317 |
---|---|
Publisher: | Manning |
Publication date: | 04/01/2016 |
Sold by: | SIMON & SCHUSTER |
Format: | eBook |
Pages: | 256 |
File size: | 5 MB |
About the Author
Evan Hahn is an active member of the Node and Express community and contributes to many open source JavaScript projects.
Table of 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
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
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 FJS 109
The syntax of EJS 109
7.3 Everything you need to know about Pug 112
The syntax of Pug 112
Layouts in Pug 113
Mixins in Pug 115
7.4 Summary 115
Part 3 Express in Context 117
8 Persisting your data with MongoDB 119
8.1 Why MongoDB? 120
How Mongo works 121
For you SQL users out there 122
Setting up Mongo 123
8.2 Talking to Mongo from Node with Mongoose 124
Setting up your project 124
Creating a user model 125
Using your model 129
8.3 Authenticating users with Passport 136
Setting up Passport 137
8.4 Summary 145
9 Testing Express applications 146
What is testing and why is it important? 147
Test-driven development 148
Cardinal rule: when in doubt, test 150
9.2 Introducing the Mocha testing framework 150
How does Node.js testing work? 151
Setting up Mocha and the Chai assertion library 151
What happens when you run your tests 152
Writing your first test with Mocha and Chai 153
Adding more tests 154
More features of Mocha and Chai 158
9.3 Testing Express servers with SuperTest 159
Testing a simple API 161
Filling in the code for your first tests 165
Testing HTML responses 166
Summary 171
10 Security 172
10.1 The security mindset 173
10.2 Keeping your code as bug-free as possible 173
Enforcing good JavaScript with JSHint 174
Halting after errors happen in callbacks 175
Perilous parsing of query strings 176
10.3 Protecting your users 178
Using HTTPS 178
Preventing cross-site scripting attacks 179
Cross-site request forgery (CSRF) prevention 182
10.4 Keeping your dependencies safe 185
Auditing the code 186
Keeping your dependencies up to date 186
Check against the Node Security Project 187
10.5 Handling server crashes 187
10.6 Various little tricks 188
No Express here 188
Preventing clickjacking 189
Keeping Adobe products out of your site 190
Don't let-browsers infer the file type 191
10.7 Summary 192
11 Deployment: assets and Heroku 193
11.1 LESS, a more pleasant way to write CSS 194
Variables 195
Functions 196
Mixins 197
Nesting 198
Includes 200
Alternatives to LESS 200
11.2 Using Browserify to require modules in the browser 200
A simple Browserify example 201
11.3 Using Grunt to compile, minify, and more 203
Installing Grunt 203
Compiling LESS with Grunt 204
Using Browserify with Grunt 207
Minifying the JavaScript with Grunt 207
Using Grunt watch 208
Other helpful Grunt tasks 209
11.4 Using connect-assets to compile LESS and CoffeeScript 209
Getting everything installed 210
Setting up the middleware 210
Linking to assets from views 211
Concatenating scripts with directives 211
11.5 Deploying to Heroku 212
Getting Heroku set up 212
Making a Heroku-ready app 213
Deploying your first app 214
Running Grunt on Heroku 215
Making your server more crash resistant 216
11.6 Summary 217
12 Best practices 218
12.1 Simplicity 219
12.2 File structure pattern 220
12.3 Locking down dependency versions 221
The simple way: eschewing optimistic versioning 222
The thorough, way: npm's shrinkwrap command 225
Upgrading and adding dependencies 226
12.4 Localized dependencies 226
Invoking commands directly 227
Executing commands from npm scripts 227
12.5 Summary 228
Appendix Other helpful modules 229
Index 231