Practical Node.js (2018版) 第10章:Getting Node.js Apps Production Ready
Getting Node.js Apps Production Ready
部署程序需要知道的方面:
- Environment variables
- Express.js in production
- Socket.IO in production(在线文章缺失这章)
- Error handling
- Node.js domains for error handling
- Multithreading with Cluster
- Multithreading with Cluster2
- Event logging and monitoring
- Building tasks with Grunt
- Locking dependencies
- Git for version control and deployments
- Running tests in Cloud with TravisCI
Environment Variables
在部署到产品环境之前,需要准备好代码。私人的信息不要放入版本控制系统。
敏感信息如API keys,密码,数据库的URIs最好储存在环境变量内。不要放到源码中。
Node.js可以容易的存取这些环境变量:
console.log (process.env.NODE_ENV,
process.env.API_KEY,
process.env.DB_PASSWORD)
然后,在程序开始前, 设置这些变量:
$ NODE_ENV=test API_KEY=XYZ DB_PASSWORD=ABC node envvar.js
一般,这些环境变量设置是部署的一部分,或者operations setup。
下一章,我们在server内处理这些变量。
Express.js in Production
使用判断语法来检测NODE_ENV变量的值,根据值来使用不同层次的服务logs。
比如开发阶段,我们需要大量的信息,但是在产品阶段,stack和exceptions可能会导致攻击,所以:
const errorHandler = require('errorhandler')
if (process.env.NODE_ENV === 'development') {
app.use(errorHandler({
dumpExceptions: true,
showStack: true
}))
} else if (process.env.NODE_ENV === 'production') {
app.use(errorHandler())
}
process.env.NODE_ENV作为环境变量,可以在terminal(bash, zsh或者其他)设置。
环境变量的语法规则是:
KEY=VALUE
//或者在整个shell session时使用 export KEY=VALUE //例子
$ NODE_ENV=production node app.js
备注:
不是很立即本章给的代码。(https://github.com/azat-co/practicalnode/blob/master/chapter10/chapter10.md)
Error handler
Multithreading with Cluster
核心模块Cluster可以多线程处理系统load。
Event Logging and Monitoring
- Monitor via dashboard and health statuses (monitoring and REPL)
- Analyze postmortems after the events have happened (Winston and Papertrail)
Building Tasks with Grunt
It performs compilations, minifications, linting, unit testing, and other important tasks for automation.
A Brief on Webpack
和Grunt的功能类似。
module.exports = {
entry: "./jsx/app.jsx",
output: {
path: __dirname + '/js',
filename: "bundle.js"
},
// ... More configurations
}
见之前的博客:
webpack基础概念
https://www.cnblogs.com/chentianwei/p/9877570.html
Locking Dependencies
使用^或者*, 或者在package.json内让version field空着,都会导致下载更高的版本,当你或谁执行npm install。
一个解决方案是,commit node_modules to git
因为即使我们锁定包A在我们的package.json内,很可能A内有一个*或者版本的范围。因此一旦更新,这个A module可能break system。
Commiting模块到你的版本控制系统。
Git for Version Control and Deployments
安装
生成SSH key.
创建本地仓库:
Running Tests in Cloud with TravisCI
使用云端跑测试。有不少选择。
Practical Node.js (2018版) 第10章:Getting Node.js Apps Production Ready的更多相关文章
- Practical Node.js (2018版) 第7章:Boosting Node.js and Mongoose
参考:博客 https://www.cnblogs.com/chentianwei/p/10268346.html 参考: mongoose官网(https://mongoosejs.com/docs ...
- Practical Node.js (2018版) 第5章:数据库 使用MongoDB和Mongoose,或者node.js的native驱动。
Persistence with MongoDB and Mongoose https://github.com/azat-co/practicalnode/blob/master/chapter5/ ...
- Practical Node.js (2018版) 第9章: 使用WebSocket建立实时程序,原生的WebSocket使用介绍,Socket.IO的基本使用介绍。
Real-Time Apps with WebSocket, Socket.IO, and DerbyJS 实时程序的使用变得越来越广泛,如传统的交易,游戏,社交,开发工具DevOps tools, ...
- Practical Node.js (2018版) 第3章:测试/Mocha.js, Chai.js, Expect.js
TDD and BDD for Node.js with Mocha TDD测试驱动开发.自动测试代码. BDD: behavior-driven development行为驱动开发,基于TDD.一种 ...
- Practical Node.js (2018版) 第8章:Building Node.js REST API Servers
Building Node.js REST API Servers with Express.js and Hapi Modern-day web developers use an architec ...
- Practical Node.js (2018版) 第4章: 模版引擎
Template Engines: Pug and Handlebars 一个模版引擎是一个库或框架.它用一些rules/languages来解释data和渲染views. web app中,view ...
- Vue.js 学习笔记 第1章 初识Vue.js
本篇目录: 1.1 Vue.js 是什么 1.2 如何使用Vue.js 本章主要介绍与Vue.js有关的一些概念与技术,并帮助你了解它们背后相关的工作原理. 通过对本章的学习,即使从未接触过Vue.j ...
- Node入门教程(7)第五章:node 模块化(下) npm与yarn详解
Node的包管理器 JavaScript缺少包结构的定义,而CommonJS定义了一系列的规范.而NPM的出现则是为了在CommonJS规范的基础上,实现解决包的安装卸载,依赖管理,版本管理等问题. ...
- Node入门教程(6)第五章:node 模块化(上)模块化演进
node 模块化 JS 诞生的时候,仅仅是为了实现网页表单的本地校验和简单的 dom 操作处理.所以并没有模块化的规范设计. 项目小的时候,我们可以通过命名空间.局部作用域.自执行函数等手段实现变量不 ...
随机推荐
- 《Mysql 分区分表》
一:分区/分表 为了什么? - 当MySQL单表的数据量过大时,数据库的访问速度会下降,需要处理大量数据,所以需要把数据分散存储. - 常用 "水平" 切分 二:MySQL常见的水 ...
- Django中一个项目使用多个数据库
在django项目中, 一个工程中存在多个APP应用很常见. 有时候希望不同的APP连接不同的数据库,这个时候需要建立多个数据库连接. 参考:http://blog.csdn.net/songfree ...
- Cartographer源码阅读(4):Node和MapBuilder对象2
MapBuilder的成员变量sensor::Collator sensor_collator_; 再次阅读MapBuilder::AddTrajectoryBuilder方法.首先构造了mappin ...
- #WEB安全基础 : HTTP协议 | 0x0 TCP/IP四层结构
学完HTML/CSS了? 做了这么多网页,但是你知道它们是怎么工作的吗? 作为你的朋友,我也对这些东西感兴趣,在写博客的同时也在和你一起学. 废话少说,进入正题 网络中的通信包括两个端分别为:客户端( ...
- DevOps“五宗罪”,这样向DevOps过渡注定会失败
云计算提供的速度响应.敏捷性和规模效应,契合了如今不断变化的数字商业环境.企业基于最新的IT技术,重构IT架构,加速产品创新和服务交付的速度,从而提高运营效率和市场占有. 不过,企业IT管理者在利用云 ...
- C#-----中使用using详解
1.using指令 using + 命名空间名字 例:using System; using System.Data; 2.using语句 定义一个范围,在范围结束时处理对象,出了这个范 ...
- HTTPConnectionPool(host='xx.xx.xx.xx', port=xx): Max retries exceeded with url:(Caused by ConnectTimeoutError(<urllib3.connection.HTTPConnection object at 0x0000015A25025EB8>...))
HTTPConnectionPool(host='xx.xx.xx.xx', port=xx): Max retries exceeded with url:(Caused by ConnectTim ...
- 【Spark-core学习之六】 Spark资源调度和任务调度
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk1.8 scala-2.10.4(依赖jdk1.8) spark ...
- Class_fourh_异常总结
使用try,catch,finally.检查指点检查点是否错误: try里填入监测的内容 catch 小括号里放类型错误 判断try里出现的错误是哪一类错误 中括号里放 输出内容 在控制台上输出错误 ...
- 隐藏非选中的checkBox
//隐藏非选中的checkBox function onlyCheckBox(){ $("#dtlTable tr:gt(0)").each(function(i) { var c ...