<br/><br/>javascript
Node.js 是什么css
Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js' package ecosystem, npm, is the largest ecosystem of open source libraries in the world.
1. JavaScript 运行时 2. 既不是语言,也不是框架,它是一个平台
Node.js 中的 JavaScripthtml
1. 没有 BOM、DOM 2. 在 Node 中为 JavaScript 提供了一些服务器级别的 API 2.1 fs 文件操做模块 2.2 http 网络服务构建模块 2.3 os 操做系统信息模块 2.4 path 路径处理模块 2.5 .....
<!-- markdown-to-slides share1.md -o index.html -s slide.css -->vue
基于 Node.js 平台,快速、开放、极简的 Web 开发框架java
简单来讲,封装了node中http核心模块,专一于业务逻辑的开发。node
安装方法webpack
npm install express --save
Koa - next generation web framework for node.js
Koa 是一个新的 web 框架,由 Express 幕后的原班人马打造, 致力于成为 web 应用和 API 开发领域中的一个更小、更富有表现力、更健壮的基石。 经过利用 async 函数,Koa 帮你丢弃回调函数,并有力地加强错误处理。 Koa 并无捆绑任何中间件, 而是提供了一套优雅的方法,帮助您快速而愉快地编写服务端应用程序。(project:koa-1.js)web
const Koa = require('koa'); const app = new Koa(); app.use(async ctx => { ctx.body = 'Hello World'; }); app.listen(3000);
<img src="http://img0.ph.126.net/77JLQd...; />shell
var http = require('http') http.createServer(function (req, res) { // 主页 if (req.url == "/") { res.end("Holle hemo!"); } // About页 else if (req.url == "/about") { res.end("Hello about!"); } }).listen('3009', 'localhost', function () { console.log('listen 3009 ....') })
project:demo-1.js Hello XXX 测试结果:express
<img src="http://img2.ph.126.net/oE0IH7...; height="200" width="600" />
var express = require('express') var app = express() app.get('/', function (req, res) { res.send('hello home ...') }) app.get('/about', function (req, res) { res.send('hello about ...') }) app.listen(3000, function () { console.log('express app is runing .....') })
project:demo-2.js Hello XXX 测试结果:
<img src="http://img1.ph.126.net/py2gHZ...; height="200" width="600" />
Routing refers to how an application’s endpoints (URIs) respond to client requests.
-请求方法
-请求路径
-请求处理函数
公开静态资源
// 访问路径 http://127.0.0.1:3000/a.js app.use(express.static('./public/')) // 访问路径 http://127.0.0.1:3000/public/a.js app.use('/public/',express.static('./public/'))
node 中存在的模块主要有:
// 加载核心模块 const path = require('path'); // 加载自定义模块 const foo = require('./fooo.js') // 加载第三方模块 node_modules const express = require('express')
node 中require加载规则:
判断模块标识
2.1 是不是核心模块 http 、fs 加载 缓存 export
2.2 是不是文件模块 ./foo.js 加载 缓存 export
2.3 是不是第三方模块 (第三方模块须要 npm install 安装)
- node_modules/art-template/ - node_modules/art-template/package.json - node_modules/art-template/package.json 中找main 做为文件加载入口 - index.js 备选项 - 进入上一级目录找 node_modules - 按照此规则依次向上查找,直到磁盘根目录找不到 报错 Can not find moudle XXX
node 中require加载规则:
<img src="http://img1.ph.126.net/rbUZEF...; height="540" width="300" />
<img src="http://img2.ph.126.net/h8Kc7e...; height="540" width="400" style="margin-left:60px"/>
nodemon《nodemon使用方法》
nodemon reload, automatically
nodemon用来监视node.js应用程序中的任何更改并自动重启服务,很是适合用在开发环境中
// 全局安装nodemon npm install -g nodemon nodemon app.js
express中没有内置的获取表单请求体的API,因此须要第三方包解析HTTP请求体
This module provides the following parsers:
使用方法:
const bodyParser = require('body-parser'); const app = express(); app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json()); app.use(function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); next(); });
router.post('/students/new', function (req, res) { console.log(req.body) Student.save(req.body, function (err) { if (err) { return res.status(500).send('Server error.') } res.redirect('/students') }) })
post 提交方式测试:
<img src="http://img1.ph.126.net/82CmNm...; width="350" />
<img src="http://img1.ph.126.net/wSMCvy...; width="350" style="margin-left:40px"/>
npm 5之前没有 package-lock.json 文件
path.join(__dirname, '../node_modules') path.basename(path[, ext])
<img src="http://img2.ph.126.net/MgfYCw...; />
在每一个模块中,除了require、export 等模块相关的API以外,还有两个特殊的成员
1. 在文件操做路径中,相对路径设计的是相对于执行node命令所在路径
2. 模块中的路径标识就是相对于当前文件模块,不受执行node命令所处路径影响
const fs = require('fs') const path = require('path') // 文件操做中的相对路径 fs.readFile('c:/a/b/a.txt', 'utf-8', function (err, data) { if (err) throw err console.log(data) }) // 文件操做中的相对路径转化为动态获取的绝对路径 fs.readFile(path.join(__dirname,'./a.txt'), 'utf-8', function (err, data) { }) // 模块中的路径标识 require('./b')
中间件(middleware) 在 Node.js 中被普遍使用,它泛指一种特定的设计模式、一系列的处理单元、过滤器和处理程序,以函数的形式存在,链接在一块儿,造成一个异步队列,来完成对任何数据的预处理和后处理。
常规的中间件模式
<img src="https://upload-images.jianshu...; />
Middleware functions are functions that have access to the request object (req), the response object (res), and the next function in the application’s request-response cycle.
中间件的本质就是请求处理方法,把用户从请求到响应的整个过程分发到多个中间件中去处理,提升代码灵活性,动态可扩展
<img src="https://timgsa.baidu.com/timg...;quality=80&size=b9999_10000&sec=1542863886302&di=8c2650a2389ce7e21e5afcf63739266a&imgtype=0&src=http%3A%2F%2Fresupload.xueda.com%2Fupload%2F55ee1c9f-971f-4e88-a708-666a1459c388%2FkX159dhLPTXl.gif" />
var express = require('express') var app = express() var myLogger = function (req, res, next) { console.log('LOGGED') next() console.log('After LOGGED') } var myLogger2 = function (req, res, next) { console.log('LOGGED2') next(); console.log('After LOGGED2') } app.use(myLogger) app.use(myLogger2) app.listen(3000, function () { console.log('express app is runing .....') })
project:demo-3.js 运行结果以下:
<img src="http://img0.ph.126.net/RJXU-z...; />
function express() { var taskArrray = [] var app = function (req, res) { var i = 0 function next() { var task = taskArrray[i++] if (!task) { return; } task(req, res, next); } next(); } // 将中间件存入数组中 app.use = function (task) { taskArrray.push(task) } return app; }
project:demo-4.js
var http = require('http') var app = express(); http.createServer(app).listen('3000', function () { console.log('listening 3000....'); }); var myLogger = function (req, res, next) { console.log('LOGGED') next() console.log('After LOGGED') } var myLogger2 = function (req, res, next) { console.log('LOGGED2') next(); console.log('After LOGGED2') } app.use(myLogger) app.use(myLogger2)
<img src="http://img0.ph.126.net/RJXU-z...; />
应用层级别中间件
路由级别中间件
错误处理中间件
内置中间件
第三方中间件
project:demo-5.js
// 不关心请求路径和请求方法的中间件 app.use(function (req, res, next) { console.log('all request must execute!!') next() }) app.use(function (req, res, next) { console.log('all request must execute 1 !!') }) // 以/XXX 开头的路径的中间件 app.use('/user/:id', function (req, res, next) { console.log('Request URL:', req.originalUrl) next() }, function (req, res, next) { console.log('Request Type:', req.method) next() }) // 严格匹配请求方法和请求路径的中间件 app.get('/aa/bb', function (req, res, next) { console.log('/aa/bb') next() })
// 内置中间件 app.use('/public/', express.static('./public/')) // 全部都匹配不到时 404 (放在最后) app.use('/', router) app.use(function (req, res, next) { res.send('This is 404 !!!!!') }) // 配置全局错误统一处理中间件 app.get('/aa/bb', function (req, res, next) { fs.readFile('c:/a/b/index.js', 'utf-8', function (err) { if (err) return next(err) }) }) app.use(function (err, req, res, next) { res.status(500).json({ err_code: 500, err_msg: err.message }) }) // 第三方级别中间件 const bodyParser = require('body-parser'); app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json());
经过应用生成器工具 express-generator 能够快速建立一个应用的骨架。
npm install express-generator -g express myapp --view=pug cd myapp npm install npm run start
project:myapp 访问http://localhost:3000/
<img src="http://img2.ph.126.net/Hdg8CX...; />
1.查看myapp目录结构
2.结合中间件分析代码
project: myapp
<img src="http://img1.ph.126.net/KyJ0a3...; height="580px" style="margin-left:400px;margin-top:-160px"/>
morgan
HTTP request logger middleware for node.js
pug
Pug is a high performance template engine heavily influenced by Haml and implemented with JavaScript for Node.js and browsers. For bug reports, feature requests and questions, open an issue. For discussion join the chat room.
h1 Pug - node template engine <h1>Pug - node template engine</h1>
// compile var fn = pug.compile('string of pug', options); var html = fn(locals); // render var html = pug.render('string of pug', merge(options, locals));
结合ccs-operation-web中 模拟接口 ./api/server.js
<img src="http://img1.ph.126.net/WcCZiU...; />
project:app.js
<img src="http://img2.ph.126.net/ODW9rX...; />
<img src="http://img1.ph.126.net/BPnIoG...; />
"scripts": { "server": "nodemon api/server.js", "dev": "webpack-dev-server --inline --progress --open --config build/webpack.dev.conf.js", // 影响ccs-operation-web/config/proxyConfig.js http://localhost:3002/api/listContracts?pin=X&all=X "devlocal": "shell-exec --colored-output \"npm run dev --local\" \"npm run server\"", }
shell-executor
A small nodejs module to execute shell commands in parallel
npm i -g shell-executor // --colored-output Use colored output in logs shell-exec --colored-output 'npm run lint' 'npm run test' 'npm run watch'
const express = require('express'); const bodyParser = require('body-parser'); const request = require('request'); const path = require('path'); const walk = require('klaw-sync'); const { origin_proxy_url, local_proxy_port, local_proxy_url } = require('../config/proxyConfig'); const app = express(); app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json()); app.use(function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); next(); }); let _existRoutes = [];
app.use( (req, res, next)=>{ const {url, body, method} = req; if (!~_existRoutes.indexOf(req.path)) { const rurl = origin_proxy_url.replace(/\/$/, '') + url; let r = method === 'POST' ? request.post({url: rurl, form: body}, (err, httpRes, reqBody)=>{ console.log(err, reqBody, body) }) : request(rurl); console.log(`本地未定义的请求,跳转到 ${method} ${rurl}`); req.pipe(r).pipe(res); return; } next(); }); //遍历本目录下的 *.api.js walk(path.resolve('./')) .filter(p=>/\.api\.js$/.test(p.path)) .map(p=>p.path) .forEach(part=>require(part)(app)); //记录注册过的路由 _existRoutes = app._router.stack.filter(s=>s.route).map(s=>s.route.path); app.listen(local_proxy_port, ()=>{ console.log(`\n\n local server running at ${local_proxy_url} \n\n`); });
klaw-sync is a Node.js recursive and fast file system walker
// 用法 const klawSync = require('klaw-sync') const paths = klawSync('/some/dir') // paths = [{path: '/some/dir/dir1', stats: {}}, {path: '/some/dir/file1', stats: {}}]
<img src="http://img1.ph.126.net/O9u0y_...; />
Request - Simplified HTTP client
// 用法 npm install request var request = require('request'); request('http://www.google.com', function (error, response, body) { console.log('error:', error); console.log('statusCode:', response && response.statusCode); console.log('body:', body); }); req.pipe(request('http://mysite.com/doodle.png')).pipe(resp)
<img src="http://img1.ph.126.net/B5PvG3...; />
express 基于 Node.js 平台,快速、开放、极简的 Web 开发框架
简单来讲,封装了node中http核心模块,专一于业务逻辑的开发。
express 核心内容 : 理解、使用中间件