持续更新中
莫名其妙,又开坑了。哈哈哈哈。
对于这里来说,不仅仅是Nodejs的内容,更多的是后端的一些知识: 网络、服务器的各项配置、端口、nginx、一个完整的后端应具有的东西:
查看: Nodejs路线图 和 awesome Nodejs 来了解整体需要的。
===========================================================
问题
- 端口可用,端口占用
- Node是单线程的, 所以每次在终端 node 一下, 就是 Node 的一个线程; 服务器可以 有很多线程。
- https://medium.freecodecamp.com/learn-node-js-with-brigadier-fluffykins-i-basics-async-sync-create-your-first-server-b9e54a45e108#.gxn155j4o
===========================================================
Nodejs版本 与 兼容性
http://node.green/ 支持度列表
需注意不同版本对 ES6 的支持度,使用工具安装和管理多个版本
- nvm
- n 多版本管理
数据请求部分:
- 跨域
- 服务器启用
- 接受请求: get post put 等等
- stream 和 文件系统 的区别
- 数据的读取和写入
CORS
基本的后端服务:
https://www.mongodb.com/blog/post/serverless-architectures-the-evolution-of-cloud-computing?jmp=twt 架构选型
- 首要是整个服务器的安全: 限制请求速率
- 授权验证
- cookie session
- 日志记录
日志记录
什么是日志?日志该记录什么?
NGINX
从WIKI中了解,Nginx(发音同engine x)是一个网页服务器,它能反向代理HTTP, HTTPS, SMTP, POP3, IMAP的协议链接,以及一个负载均衡器和一个HTTP缓存。
- 反向代理
- 负载均衡
- 页面缓存
- URL重写
- 读写分离
- 网络配置: 端口、映射、NAT
- 二级域名
WIKI:
- http://wiki.jikexueyuan.com/project/nginx/overwise.html 淘宝的教程,官网我看不了,只好找到这个
- https://gist.github.com/lwxyfer/58148d59c1163cf71c6a445a02ff95f0 nginx配置,简单实用,不做做
- https://gist.github.com/plentz/6737338 nginx最佳配置
- https://zh.wikipedia.org/wiki/网络地址转换 NAT
基本使用的话,可以直接按照最近配置就好,使用 HTTP2 和 HTTPS 的话,也可以直接搜索到的,不用记住。
二级域名配置的话,若使用 Node,需要注意 本地地址 和 端口 的匹配。然后把site-abaible 下 nginx 配置文件 ln -s 到 site-enable。 具体的话,只有遇到问题再搜索喽。
# read more here http://tautt.com/best-nginx-configuration-for-security/
# don't send the nginx version number in error pages and Server header
server_tokens off;
# config to don't allow the browser to render the page inside an frame or iframe
# and avoid clickjacking http://en.wikipedia.org/wiki/Clickjacking
# if you need to allow [i]frames, you can use SAMEORIGIN or even set an uri with ALLOW-FROM uri
# https://developer.mozilla.org/en-US/docs/HTTP/X-Frame-Options
add_header X-Frame-Options SAMEORIGIN;
# when serving user-supplied content, include a X-Content-Type-Options: nosniff header along with the Content-Type: header,
# to disable content-type sniffing on some browsers.
# https://www.owasp.org/index.php/List_of_useful_HTTP_headers
# currently suppoorted in IE > 8 http://blogs.msdn.com/b/ie/archive/2008/09/02/ie8-security-part-vi-beta-2-update.aspx
# http://msdn.microsoft.com/en-us/library/ie/gg622941(v=vs.85).aspx
# 'soon' on Firefox https://bugzilla.mozilla.org/show_bug.cgi?id=471020
add_header X-Content-Type-Options nosniff;
# This header enables the Cross-site scripting (XSS) filter built into most recent web browsers.
# It's usually enabled by default anyway, so the role of this header is to re-enable the filter for
# this particular website if it was disabled by the user.
# https://www.owasp.org/index.php/List_of_useful_HTTP_headers
add_header X-XSS-Protection "1; mode=block";
# with Content Security Policy (CSP) enabled(and a browser that supports it(http://caniuse.com/#feat=contentsecuritypolicy),
# you can tell the browser that it can only download content from the domains you explicitly allow
# http://www.html5rocks.com/en/tutorials/security/content-security-policy/
# https://www.owasp.org/index.php/Content_Security_Policy
# I need to change our application code so we can increase security by disabling 'unsafe-inline' 'unsafe-eval'
# directives for css and js(if you have inline css or js, you will need to keep it too).
# more: http://www.html5rocks.com/en/tutorials/security/content-security-policy/#inline-code-considered-harmful
add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://ssl.google-analytics.com https://assets.zendesk.com https://connect.facebook.net; img-src 'self' https://ssl.google-analytics.com https://s-static.ak.facebook.com https://assets.zendesk.com; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com https://assets.zendesk.com; font-src 'self' https://themes.googleusercontent.com; frame-src https://assets.zendesk.com https://www.facebook.com https://s-static.ak.facebook.com https://tautt.zendesk.com; object-src 'none'";
server {
listen 443 ssl default deferred;
server_name .forgott.com;
ssl_certificate /etc/nginx/ssl/star_forgott_com.crt;
ssl_certificate_key /etc/nginx/ssl/star_forgott_com.key;
# enable session resumption to improve https performance
# http://vincent.bernat.im/en/blog/2011-ssl-session-reuse-rfc5077.html
ssl_session_cache shared:SSL:50m;
ssl_session_timeout 5m;
# Diffie-Hellman parameter for DHE ciphersuites, recommended 2048 bits
ssl_dhparam /etc/nginx/ssl/dhparam.pem;
# enables server-side protection from BEAST attacks
# http://blog.ivanristic.com/2013/09/is-beast-still-a-threat.html
ssl_prefer_server_ciphers on;
# disable SSLv3(enabled by default since nginx 0.8.19) since it's less secure then TLS http://en.wikipedia.org/wiki/Secure_Sockets_Layer#SSL_3.0
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
# ciphers chosen for forward secrecy and compatibility
# http://blog.ivanristic.com/2013/08/configuring-apache-nginx-and-openssl-for-forward-secrecy.html
ssl_ciphers "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4";
# enable ocsp stapling (mechanism by which a site can convey certificate revocation information to visitors in a privacy-preserving, scalable manner)
# http://blog.mozilla.org/security/2013/07/29/ocsp-stapling-in-firefox/
resolver 8.8.8.8;
ssl_stapling on;
ssl_trusted_certificate /etc/nginx/ssl/star_forgott_com.crt;
# config to enable HSTS(HTTP Strict Transport Security) https://developer.mozilla.org/en-US/docs/Security/HTTP_Strict_Transport_Security
# to avoid ssl stripping https://en.wikipedia.org/wiki/SSL_stripping#SSL_stripping
add_header Strict-Transport-Security "max-age=31536000; includeSubdomains;";
# ... the rest of your configuration
}
# redirect all http traffic to https
server {
listen 80;
server_name .forgott.com;
return 301 https://$host$request_uri;
}
Express
整个框架的思想,路由的详细划分,可以从cNode社区学习这些内容
http://expressjs.com/zh-cn/advanced/best-practice-security.html 安全实践
Restful 和 前后端完全分离
- https://cnodejs.org/topic/52d3e40be20b7c82146b0f1b
- http://blog.fens.me/nodejs-restify/
- https://cnodejs.org/topic/55778225c4e7fbea6e9a3357
- https://github.com/moajs/moa-api
- https://cnodejs.org/topic/557d647216839d2d53936351
- https://www.zhihu.com/question/31414479 讨论
- https://bourgeois.me/rest/
- https://www.ibm.com/developerworks/cn/web/1211_zuochao_nodejsrest/
- http://web.jobbole.com/85886/
- https://gist.github.com/iahu/7a6d734ee0e6cdfd8509
- https://cnodejs.org/topic/55e5955948d9b9a27247c9a1
安全
安全才是首要的
常见的攻击:
- 回放攻击
- csrf攻击:跨站请求伪造(Cross-site request forgery)
- XSS:跨站脚本(Cross-site scripting),在网页输入中嵌入脚本
- dom based
- stored xss:发帖存储相关代码
- 中间人攻击:实现会话劫持
攻击的基本思想
-
利用cookies:
-
非法获取:xss可以读取cookies,获取用户信息
-
模拟某个请求浏览器会自动带上cookies(csrf)
-
在外部(恶意)网站模拟被攻击网站
- 虽然浏览器禁止跨域访问,但是还是可以通过其他方法访问
的src
的src
预防
- 虽然浏览器禁止跨域访问,但是还是可以通过其他方法访问
-
保护好cookies
-
使用http-only
-
防止非法代码注入xss
-
对输入校验
-
统一对输入encode,防止嵌入
-
防止csrf
-
对请求来源进行验证—下发表单时加入hide input包含csrf token,确保以后提交的表单是自己下发的,不是伪造的。
-
中间人攻击:https
安全设置
- https://www.npmjs.com/package/secure-filters
- https://github.com/chriso/validator.js
- https://blog.risingstack.com/node-js-security-checklist/ 文章,很全
数据库
mongodb
数据库这块的话,mongodb简单的用用也是很easy的。但当然深入就是整个数据库的设计了。
http://www.runoob.com/mongodb/mongodb-connections.html
- 安装使用:
- 安装操作系统下底层文件: 可在 shell 连接和使用
- 安装 Node 驱动 : 基于 Node 操作和使用
- 可使用 mongoose : 封装操作 mongodb
- CURD: 创建(Create)、更新(Update)、查询(Retrieve)和删除(Delete)
- 数据库的基础知识: 清楚数据结构和理解数据存储
- database
- collection
- document
- field
- index
- primary key
- 关于数据库设计的基础知识
参考:
- http://www.runoob.com/mongodb/mongodb-create-database.html
- http://wiki.jikexueyuan.com/project/mongodb/mongodb-create-collection.html
- http://docs.mongoing.com/manual-zh/core/databases-and-collections.html 官方文档
基本的命令
CLI 下基本命令使用
help #mongodb支持的命令
show dbs #查看所有数据库
use test
db #查看当前连接在哪个数据库
show collections #查看所有的collection
db.help() #当前数据库支持哪些方法
db.user.help() #当前数据库下的表或者表collection支持的方法
db.foo.find() #查找所有
db.foo.findOne() #查找一条记录
db.foo.find({'msg':'Hello 1'}).limit(10) #根据条件检索10条记录
db.mail_addr.drop() #删除collection
db.dropDatabase() #删除当前的数据库
db.foo.remove({'yy':5}) #删除yy=5的记录
db.foo.remove() #删除所有的记录
#存储嵌套的对象
db.foo.save({'name':'ysz','address':{'city':'beijing','post':100096},'phone':[138,139]})
#存储数组对象
db.user_addr.save({'Uid':'yushunzhi[@sohu](/user/sohu).com','Al':['test-1[@sohu](/user/sohu).com','test-2[@sohu](/user/sohu).com']})
#根据query条件修改,如果不存在则插入,允许修改多条记录
db.foo.update({'yy':5},{'$set':{'xx':2}},upsert=true,multi=true)
mongoose
感谢开源的轮子啊,大量开源的优秀的轮子减少了很多开发时间。 知悉基本概念,就能够快速入手。
- Schema : 一种以文件形式存储的数据库模型骨架,不具备数据库的操作能力
- Model : 由Schema发布生成的模型,具有抽象属性和行为的数据库操作对
- Entity : 由Model创建的实体,他的操作也会影响数据库
相关参考:
错误与异常处理
对比前端来讲,后端的错误处理就显得很重要的,如果出错没有响应的对策,那么整个服务就会崩溃,我们需要建立相应的错误处理机制。
- https://segmentfault.com/a/1190000002741935
- https://strongloop.com/strongblog/robust-node-applications-error-handling/?_ga=1.24936442.1196661681.1453521338
- http://expressjs.com/zh-cn/advanced/best-practice-performance.html
- http://expressjs.com/zh-cn/guide/error-handling.html
- https://cnodejs.org/topic/558652fe01d3ce0d73d68fef
- http://www.alloyteam.com/2013/12/node-js-series-exception-caught/
- http://code.oneapm.com/nodejs/2015/04/13/nodejs-errorhandling/ ONEAPM : 错误的产生和处理
测试
- 单元测试
- 压力测试
- Node 兼容性测试,由于版本差异,所以需要进行测试
Node 的进程与异步
子进程
- 创建子进程
- 直接执行shell
- 执行 shell 脚本
https://tangguangyao.github.io/2015/12/16/使用node子进程spawn-exec踩过的坑/ 一些问题
shell 与Node
https://github.com/qinjx/30min_guides/blob/master/shell.md shell脚本入门
http://www.infoq.com/cn/articles/yph-shell-meet-nodejs
http://www.runoob.com/linux/linux-shell.html
https://github.com/polotek/procstreams 一些第三方的交互库
http://blog.useasp.net/archive/2014/06/02/summary-of-the-special-characters-in-shell-on-linux.aspx shell下的一些特殊符号,以及高效编程
shell的定义呢,从WIKI中可以清晰明白。shell不仅是命令行语言,而且是一门编程语言。
语法要求严格,尤其空格,shell非编译,是直接读取的然后执行。
异步 与 流程控制
Q
- 什么时候使用异步,如何判定
WORDS
- 回调
- 异步
- 非阻塞
- 流程控制
- co:一个库 ?
- async / await
- generator
- promise
异常 / 错误 : exception error : 怎么处理错误,何时处理
加密 与 算法
哈希算法,是指将任意长度的二进制值映射为较短的固定长度的二进制值,这个小的二进制值称为哈希值。哈希值是一段数据唯一且极其紧凑的数值表示形式。典型的哈希算法包括 ‘md5′,’sha’,’sha1′,’sha256′,’sha512′,’RSA-SHA’。
简单统计下就 md5 最快了,虽然可以碰撞
http://blog.fens.me/nodejs-crypto/ 不同算法的简介
文章
https://github.com/DoubleSpout/threadAndPackage/blob/master/chapter.7.thread_and_process.md 线程与进程区分
https://cnodejs.org/topic/55fac7eced1da72438e33a7d 环境变量配置
普通的 NODE_ENV 使用 cross-env 就可以了。
Node事件轮训的机制: