具体描述
				
				
					内容简介
   MEAN 是最流行的Web 开发工具的集合,包括MongoDB、Express、AngularJS 和Node.js。本书从MEAN 的核心框架开始,详细阐述了每一种框架的关键概念,如何正确地设置它们,以及如何用流行的模块把它们连接在一起。通过本书的实例练习,你能搭建自己的MEAN 应用架构,通过添加认证层,开发MVC 架构支持自己的项目开发。最后,你将学会使用不同的工具和框架加快你的日常开发进程。
本书适合对利用MEAN 开发现代Web 应用感兴趣的Web 开发者或JavaScript 全栈开发者阅读。     
作者简介
   Amos Q. Haviv 软件工程师,技术顾问,MEAN.IO和MEAN.JS的创建者。Amos有近十年的全栈开发经验,曾就职于多个创业公司和企业。过去的三年中,Amos一直在使用JavaScript的全栈解决方案进行开发,包括Node.js和MongoDB,以及AngularJS一类的前端MVC框架。2013年,他创建了MEAN应用的第一个样板MEAN.IO,目前在www.meanjs.org继续开发MEAN解决方案。他还在各类会议上做一些Web前沿技术的演讲。此外,他还为多家公司的开发团队提供指导。     
内页插图
          目录
   第1 章 MEAN 简介   1
1.1 三层Web 应用开发  1
1.2 JavaScript 的演进  2
1.3 MEAN 简介  3
1.4 安装MongoDB  4
1.4.1 在Windows 上安装MongoDB   5
1.4.2 在Mac OS X 和Linux 上安装MongoDB  7
1.4.3 使用MongoDB 命令行工具  8
1.5 安装Node.js   9
1.5.1 在Windows 上安装Node.js  10
1.5.2 在Mac OS X 上安装Node.js  11
1.5.3 在Linux 上安装Node.js   12
1.5.4 运行Node.js  12
1.6 NPM 简介  13
1.7 总结  18
第2 章 Node.js 入门  19
2.1 Node.js 简介  19
2.1.1 JavaScript 事件驱动编程  20
2.1.2 Node.js 事件驱动编程  22
2.2 JavaScript 闭包  23
2.3 Node 模块  24
2.3.1 CommonJS 模块  24
2.3.2 Node.js 核心模块  26
2.3.3 Node.js 第三方模块  26
2.3.4 Node.js 文件模块  26
2.3.5 Node.js 文件夹模块  27
2.4 Node.js Web 应用开发  27
2.5 总结  34
第3 章 使用Express 开发Web 应用  35
3.1 Express 简介  35
3.2 Express 安装  36
3.3 创建第一个Express 应用  36
3.4 应用、请求和响应对象  37
3.4.1 应用对象  37
3.4.2 请求对象  38
3.4.3 响应对象  38
3.5 外部的中间件  39
3.6 实现MVC 模式  40
3.7 Express 应用配置  48
3.8 渲染视图  51
3.8.1 配置视图系统  51
3.8.2 EJS 视图渲染  53
3.9 静态文件服务  53
3.10 配置会话  55
3.11 总结  57
第4 章 MongoDB 入门  58
4.1 NoSQL 简介58
4.2 MongoDB 简介  60
4.3 MongoDB 的关键特性  61
4.3.1 BSON 格式  61
4.3.2 MongoDB 即席查询  61
4.3.3 MongoDB 索引  62
4.3.4 MongoDB 副本集  63
4.3.5 MongoDB 分片  64
4.4 MongoDB 命令行工具  65
4.5 MongoDB 数据库  66
4.6 MongoDB 集合  66
4.7 MongoDB 增删改查操作  67
4.7.1 创建新文档  67
4.7.2 读取文档  68
4.7.3 更新已有文档  69
4.7.4 删除文档  70
4.8 总结  71
第5 章 Mongoose 入门  72
5.1 Mongoose 简介  72
5.1.1 安装Mongoose   72
5.1.2 连接MongoDB  73
5.2 理解Mongoose 的模式  74
5.2.1 创建User 模式与模型  74
5.2.2 注册USer 模型  75
5.2.3 使用save()创建新文档  75
5.2.4 使用find()查找多个文档  77
5.2.5 使用findOne()读取单个文档  79
5.2.6 更新已有文档  80
5.2.7 删除已有文档  81
5.3 扩展Mongoose 模式  82
5.3.1 定义默认值  82
5.3.2 使用模式修饰符  83
5.3.3 增加虚拟属性  85
5.3.4 使用索引优化查询  85
5.4 模型方法自定义  86
5.4.1 自定义静态方法  86
5.4.2 自定义实例方法  87
5.5 模型的校验  87
5.5.1 预定义的验证器  87
5.5.2 自定义的验证器  89
5.6 使用Mongoose 中间件  89
5.6.1 预处理中间件  89
5.6.2 后置处理中间件  90
5.7 使用Mongoose DBRef  90
5.8 总结  91
第6 章 使用Passport 模块管理用户权限  92
6.1 Passport 简介  92
6.1.1 安装  92
6.1.2 配置  93
6.2 理解Passport 策略   95
6.2.1 使用Passport 的本地策略   95
6.2.2 修改User 模型   97
6.2.3 创建身份验证视图   99
6.2.4 修改用户控制器   101
6.2.5 添加用户路由   105
6.3 理解Passport 的OAuth 策略   107
6.4 总结   118
第7 章 AngularJS 入门   119
7.1 AngularJS 简介   119
7.2 AngularJS 的核心概念   119
7.2.1 核心模块   120
7.2.2 模块   120
7.2.3 双向数据绑定   121
7.2.4 依赖注入   122
7.2.5 AngularJS 指令   123
7.2.6 AngularJS 应用的引导   124
7.3 安装AngularJS   125
7.3.1 Bower 包管理器   125
7.3.2 配置Bower    126
7.3.3 使用Bower 安装AngularJS    126
7.3.4 配置AngularJS   127
7.4 AngularJS 应用的结构   127
7.5 引导AngularJS 应用   130
7.6 AngularJS 的MVC 实体   131
7.6.1 视图   132
7.6.2 控制器和scope   133
7.7 AngularJS 路由   135
7.7.1 安装ngRoute 模块   136
7.7.2 配置URL 模式   137
7.7.3 AngularJS 应用路由   137
7.8 AngularJS 服务   139
7.8.1 预置服务   139
7.8.2 自定义服务   140
7.8.3 服务的使用   141
7.9 管理AngularJS 的身份验证   141
7.9.1 将user 对象填充到视图   141
7.9.2 添加身份验证服务   142
7.9.3 使用身份验证服务   144
7.10 总结  144
第8 章 创建MEAN 的CURD 模块   145
8.1 CURD 模块简介  145
8.2 配置Express 组件  145
8.2.1 创建Mongoose 模型146
8.2.2 建立Express 控制器  147
8.2.3 编写Express 路由  152
8.2.4 配置Express 应用  153
8.3 ngResource 模块简介  154
8.3.1 安装ngResource 模块  154
8.3.2 使用$resource 服务  156
8.4 实现AngularJS 的MVC 模块  157
8.4.1 创建模块服务  157
8.4.2 建立模块控制器  158
8.4.3 实现模块视图  161
8.4.4 编写AngularJS 路由  164
8.5 最终实现  164
8.6 总结  166
第9 章 基于Socket.io 的实时通信   167
9.1 WebSockets 简介  167
9.2 Socket.io 简介  168
9.2.1 Socket.io 服务器端对象  169
9.2.2 Socket.io 客户端对象  171
9.2.3 Socket.io 的事件  171
9.2.4 Socket.io 命名空间  174
9.2.5 Socket.io 的房间  175
9.3 Socket.io 的安装  176
9.3.1 配置Socket.io 的服务器  177
9.3.2 配置Socket.io 的会话  178
9.4 使用Socket.io 创建聊天室  182
9.4.1 设置聊天服务器的事件处理程序  182
9.4.2 在AngularJS 中创建Socket服务  184
9.4.3 控制器  185
9.4.4 视图  186
9.4.5 路由  186
9.4.6 实现  187
9.5 总结  189
第10 章 MEAN 应用的测试   190
10.1 JavaScript 测试简介  190
10.1.1 TDD、BDD 和单元测试  191
10.1.2 测试框架  192
10.1.3 断言库  192
10.1.4 测试执行过程管理工具  192
10.2 Express 应用测试  193
10.2.1 Mocha 简介  193
10.2.2 Should.js 简介  194
10.2.3 SuperTest 简介  194
10.2.4      
前言/序言
       
				 
				
				
					Modern Web Architecture: A Practical Guide to Building Scalable and Responsive Applications  In today's rapidly evolving digital landscape, the demand for robust, dynamic, and user-friendly web applications has never been higher. Whether you are a seasoned developer looking to refine your skillset or an aspiring programmer eager to dive into the world of full-stack development, understanding the foundational principles and practical implementation of modern web architectures is paramount. This comprehensive guide, "Modern Web Architecture: A Practical Guide to Building Scalable and Responsive Applications," is meticulously crafted to equip you with the knowledge and hands-on experience necessary to construct sophisticated web solutions that not only meet but exceed user expectations.  This book steers clear of a prescriptive, single-technology approach, instead focusing on the why and how behind architectural decisions that lead to resilient and efficient web systems. We will embark on a journey that explores the core concepts underpinning modern web development, emphasizing best practices and adaptable strategies that can be applied across a multitude of technology stacks. Our aim is to foster a deep understanding of the underlying principles, enabling you to make informed choices and architect solutions that are both future-proof and tailored to specific project requirements.  Part 1: Laying the Foundation – Understanding Core Web Concepts  Before we delve into specific implementation details, it is crucial to establish a strong theoretical bedrock. This section will demystify the fundamental building blocks of the web and the essential concepts that drive modern application development.     The Anatomy of a Web Application: We begin by dissecting the typical components of a web application, from the client-side interface that users interact with to the server-side logic that processes requests and the persistent storage that holds vital data. Understanding the distinct roles and interactions of these components is the first step towards effective architectural design. We will explore the client-server model, HTTP requests and responses, and the various layers involved in delivering a seamless user experience.     Client-Side Rendering vs. Server-Side Rendering vs. Static Site Generation: A critical architectural decision lies in how your content is rendered. This chapter will provide a detailed exploration of these three dominant rendering strategies.        Client-Side Rendering (CSR): We will analyze how JavaScript frameworks and libraries enable dynamic content loading and manipulation directly within the user's browser. We'll discuss its advantages for highly interactive applications, its potential SEO challenges, and techniques to mitigate them, such as dynamic imports and pre-rendering.        Server-Side Rendering (SSR): This section will delve into the benefits of generating HTML on the server, leading to faster initial page loads and improved SEO. We will examine common SSR patterns, the trade-offs involved, and when SSR becomes the optimal choice for your application.        Static Site Generation (SSG): We will explore the power of pre-rendering entire websites at build time, resulting in unparalleled performance and security. This chapter will cover the use cases for SSG, its integration with content management systems, and its suitability for content-heavy websites with infrequent updates.     Understanding APIs and Their Role in Modern Architectures: APIs (Application Programming Interfaces) are the connective tissue of modern web applications. This chapter will provide a thorough grounding in API design principles, focusing on RESTful principles and the emerging GraphQL paradigm.        RESTful APIs: We will break down the core tenets of REST (Representational State Transfer), including resources, statelessness, and uniform interfaces. Practical examples will illustrate how to design and consume RESTful services effectively.        GraphQL: We will introduce the concepts behind GraphQL, highlighting its advantages in enabling clients to request precisely the data they need, thereby reducing over-fetching and under-fetching. We will compare and contrast GraphQL with REST, discussing their respective strengths and ideal use cases.     Data Persistence Strategies: Effectively managing and storing data is fundamental to any web application. This section will provide an overview of various data persistence options, their characteristics, and when to employ them.        Relational Databases: We will revisit the principles of relational databases (e.g., PostgreSQL, MySQL), focusing on schema design, normalization, and query optimization.        NoSQL Databases: This chapter will explore the diverse world of NoSQL databases (e.g., MongoDB, Redis, Cassandra), categorizing them by their data models (document, key-value, column-family, graph) and discussing their advantages for specific use cases, such as handling unstructured data or achieving massive scalability.  Part 2: Designing for Scalability and Performance  Building an application that works well for a handful of users is one thing; building one that can handle thousands or millions is an entirely different challenge. This section focuses on the architectural patterns and techniques that enable your web applications to grow seamlessly and perform optimally under heavy load.     Architectural Patterns for Scalability: We will explore various architectural styles that lend themselves to scalable applications.        Monolithic Architecture: While often a starting point, we will analyze its limitations as an application grows and discuss strategies for managing monolithic applications effectively before considering a move to more distributed systems.        Microservices Architecture: This chapter will provide an in-depth examination of the microservices approach, where applications are built as a suite of small, independent services that communicate with each other. We will discuss the benefits of microservices, such as independent deployment, technology diversity, and fault isolation, alongside the complexities they introduce, including inter-service communication and distributed data management.        Service-Oriented Architecture (SOA): We will also touch upon SOA as a precursor to microservices, highlighting its emphasis on reusable services and enterprise integration.     Load Balancing and High Availability: Ensuring that your application remains accessible and responsive even during peak traffic requires intelligent load distribution and redundancy. This chapter will cover:        Load Balancer Types: We will explore different load balancing algorithms (e.g., round robin, least connections, IP hash) and hardware vs. software load balancers.        High Availability Strategies: This section will discuss techniques for building fault-tolerant systems, including redundant servers, failover mechanisms, and disaster recovery planning.     Caching Strategies for Performance Optimization: Reducing the time it takes to retrieve data is crucial for a snappy user experience. We will explore various caching layers:        Browser Caching: Understanding how to leverage browser caching to serve static assets quickly.        CDN (Content Delivery Network): Discussing the role of CDNs in distributing static content geographically closer to users.        Server-Side Caching: This chapter will delve into techniques like in-memory caching (e.g., Redis, Memcached) and database query caching to reduce the load on your backend.     Asynchronous Processing and Message Queues: For operations that are time-consuming or do not require immediate user feedback, asynchronous processing is essential. This section will introduce:        Message Queues: We will explore the architecture and benefits of message queues (e.g., RabbitMQ, Kafka, SQS) for decoupling components, handling background tasks, and enabling event-driven architectures.        Background Jobs and Workers: This chapter will cover patterns for managing and executing background tasks, ensuring your main application threads remain responsive.  Part 3: Building Responsive and User-Centric Interfaces  The front-end experience is often the first and most lasting impression a user has of your application. This section is dedicated to building interfaces that are not only visually appealing but also highly responsive, accessible, and adaptable to a wide range of devices and screen sizes.     Modern Front-End Frameworks and Libraries: While this book avoids a single prescriptive stack, understanding the landscape of front-end development is vital. We will discuss the principles and benefits behind popular frameworks (without deep-diving into specific syntax, allowing for broad applicability) that facilitate the creation of dynamic and maintainable user interfaces. This includes concepts like component-based architecture, declarative programming, and state management.     Responsive Design Principles and Techniques: Ensuring your application looks and functions flawlessly on desktops, tablets, and mobile devices is non-negotiable. This chapter will cover:        Fluid Grids and Flexible Images: Understanding how to create layouts that adapt to different screen resolutions.        Media Queries: Mastering the use of CSS media queries to apply styles conditionally based on device characteristics.        Mobile-First Design: Exploring the benefits of designing for smaller screens first and progressively enhancing for larger ones.     Progressive Web Apps (PWAs): This section will introduce the concept of Progressive Web Apps, which bridge the gap between web and native applications, offering features like offline access, push notifications, and home screen installation. We will discuss the technologies that enable PWAs and their advantages for user engagement.     Accessibility (A11y) Best Practices: Building inclusive applications means ensuring they are usable by everyone, including individuals with disabilities. This chapter will cover:        Semantic HTML: Understanding the importance of using HTML elements correctly for better screen reader compatibility.        ARIA Attributes: Learning how to use ARIA (Accessible Rich Internet Applications) attributes to enhance the accessibility of dynamic content.        Keyboard Navigation and Focus Management: Ensuring users can navigate and interact with your application using only a keyboard.  Part 4: Deployment, Monitoring, and Maintenance  A well-architected application is only effective if it can be reliably deployed, monitored, and maintained over its lifecycle. This final section equips you with the knowledge to bring your creations to life and keep them running smoothly.     DevOps Principles and Practices: This chapter will introduce the core tenets of DevOps, emphasizing collaboration, automation, and continuous improvement throughout the software development lifecycle.     Containerization and Orchestration: We will explore the benefits of containerization (e.g., Docker) for packaging applications and their dependencies, ensuring consistent environments from development to production. We will also touch upon container orchestration tools (e.g., Kubernetes) for managing and scaling containerized applications.     Continuous Integration and Continuous Deployment (CI/CD): This section will detail the processes and tools that enable automated building, testing, and deployment of applications, significantly accelerating release cycles and reducing manual errors.     Application Monitoring and Logging: Understanding the health and performance of your application in production is crucial. This chapter will cover:        Key Metrics to Monitor: Identifying essential performance indicators (e.g., response times, error rates, resource utilization).        Logging Strategies: Implementing effective logging to capture errors and understand application behavior.        Alerting and Incident Response: Setting up alerts to be notified of critical issues and establishing procedures for responding to incidents.     Security Considerations in Web Architecture: Security is not an afterthought; it must be woven into the fabric of your architectural design. This chapter will address:        Common Web Vulnerabilities: An overview of prevalent threats like XSS, CSRF, SQL Injection, and authentication bypass.        Secure Coding Practices: Guidelines for writing code that minimizes security risks.        Authentication and Authorization Strategies: Implementing robust mechanisms to verify user identities and control access to resources.        Data Encryption: Protecting sensitive data both in transit and at rest.  "Modern Web Architecture: A Practical Guide to Building Scalable and Responsive Applications" is more than just a technical manual; it is a strategic companion designed to empower you with the understanding and foresight needed to navigate the complexities of modern web development. By focusing on fundamental principles and adaptable strategies, this book aims to provide a lasting foundation for building web applications that are not only functional but also resilient, performant, and a joy for users to interact with, regardless of the specific tools you ultimately choose to employ.