express后端项目构建,爬虫

1.使用Node.js制作一个压缩包

制作一个压缩包的步骤
  • 先有一个文件
  • 读取文件
  • 制作压缩包
  • 将读取的数据放入到压缩包中

const zlib = require('zlib')
const fs = require('fs')
//创建可读的流
const inp = fs.createReadStream('yyb.txt')
//创建空压缩包
const gzip = zlib.createGzip()
//创建可写的流
const out = fs.createWriteStream('yyb.txt.gz')
inp.pipe(gzip).pipe(out)

linux命令

  1. mkdir 创建文件夹
  2. vim 简写 vi 创建文件的
  3. rm -rf 文件名称 删除
  4. 递归删除
    find ./ -name ‘文件名称’ -print -exec rm -rf {} ;
    举例: find ./ -name ‘node_modules’ -print -exec rm -rf {} ;
  5. cat 文件名称 查看某个文件的内容

前端渲染 vs 后端渲染

  1. 前端拿到数据之后, 前端来渲染数据
  2. 后端来进行渲染( express )
  3. 浏览器 -》 开发者工具-》 network
  1. headers
  2. general
  3. responseHeaders
  4. requestHeaders
  5. query string paramters
  6. Form data(表单提交的)

node.js http.get 后端爬虫

数据抓取 ---》 数据清洗 ---》 数据格式整理---》 发送前台(web服务器)

反爬虫:

数据清洗的时候做,数据中用一个其他类型格式数据

7. events

Emitter.prototype 继承过来了 on emit

const EventEmitter = require('events');
// es6 类的继承
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter();
console.log( EventEmitter.prototype)
myEmitter.on('event', () => {
console.log('an event occurred!');
});
myEmitter.emit('event')

8. fs/ zlib/ stream

10. 前端异步流程控制工具( 所有的 )

1. Promise
https://blog.csdn.net/MrJavaweb/article/details/79475949
2. Generator
https://www.cnblogs.com/imwtr/p/5913294.html
3. Async-await
4. Node.js 中的nextTick()和setimmediate()
https://www.cnblogs.com/5ishare/p/5268273.html
5. async库
https://caolan.github.io/async/

参考文档
Event-loop
http://www.ruanyifeng.com/blog/2014/10/event-loop.html?bsh_bid=983729729
史上最易读懂的 Promise/A+ 完全实现
https://zhuanlan.zhihu.com/p/21834559

npm script( npm脚本 )

1. 概念:
npm 允许在package.json文件里面,使用scripts字段定义脚本命令。
2. 常用使用:
1. npm脚本就相当于一个 电器的使用说明书
2. npm脚本运行使用 npm run
3. npm脚本可以简写,但是只有特定几个
npm start -> npm run start
npm stop -> npm run stop
npm text -> npm run test
npm restart -> npm run stop & npm run start

12. express 构建后端项目

1. 安装生成器
npm install express-generator -g
2. 使用
express options 项目名称
3. 创建项目
举例: express 04-express-project
4. 目录内容
bin
public
routes
views
app.js
package.json
5. 项目的启动
1. 先进入项目
cd 项目名称
2. 安装项目需要的依赖
npm install
3. 项目启动(说明书 package.json 中 scripts脚本)
npm start

猜你喜欢

转载自blog.csdn.net/LionKing0823/article/details/89162557
今日推荐