赞
踩
什么是服务端:
服务端的职责:
服务端的表现形式:
服务端处理数据返回前端的返回格式:
路由是什么:
Chrome V8 引擎:
运行时:
NodeJs 出现之前:
NodeJs 出现之后:
如何使用 NodeJs 做服务端:
node -v
npm -v
// 在控制台编写执行 js 代码,使用 node 命令进入 js 运行环境
node
// 在控制台输入 node + js文件,是执行该 js 文件的代码
node index.js
npm install -g cnpm --registry=https://registry.npm.taobao.org
// 安装 n
npm install -g n
// 查看版本
n -V
// 安装最新稳定版本
n stable
// or 安装指定版本
n 9.10.0
npm 是什么:
开始使用 npm:
"script": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon index.js"
}
- -save 和 --save-dev 的区别:
"dependencies": {
"lodash": "^4.17.15"
}
"devDependencies": {
"nodemon": "^2.0.2"
}
sudo npm install -g nrm
npm install -g nrm
nrm -V
// or
nrm --version
nrm ls
nrm test 镜像源名称
nrm use 镜像源名称
nrm current
nrm add 镜像源名称 镜像源链接
nrm del 镜像源名称
ES6 Module 回顾:
CommonJS 语法介绍:
require(…) 的三个层级:
CommonJS 和 ES6 Module 的区别:
为何要使用模块化:
commonjs 使用示例:
// utils.js // 单个 function sum(a, b) { return a + b; } module.exports = sum // 多个 function sum(a, b) { return a + b; } function test() { console.log("this is test"); } module.exports = { sum, test }
// index.js
// 单个
const sum = require('./utils.js'). // .js 可写可不写
const result = sum(10, 20);
console.log(result); // 30
// 多个
const { sum, test } = require('./utils');
const result = sum(10, 25);
console.log(result); // 35
test(); // this is test
什么是 debug:
debug 的重要性:
inspect 调试法:
// 9229 是默认端口号
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon --inspect=9229 index.js"
}
// 示例 const http = require('http') const a = undefined const server = http.createServer((req, res) => { debugger const url = req.url const path = url.split('?')[0] a() res.end(path) }) server.listen(3000)
nodejs 启动 web 服务:
nodejs 如何监听 http 请求:
const http = require('http')
const server = http.createServer((req, res) => {
// 对监听到的请求进行处理
// 如果此时请求地址为:http://localhost:3000/insex.html
const url = req.url
console.log(url) // 则此处得到的 url 为:/index.html
// res 将服务端的响应返回给前端
res.end('返回的内容')
})
server.listen(3000) // 可以监听 http 请求
路由包含什么:
nodejs 定义路由:
拿到 req 中的 url 和 method:
const http = require('http') const server = http.createServer((req, res) => { // 获取到请求地址中的 url const url = req.url // 这一步是为了去掉 url 中的参数,从而获得真正的 url const path = url.split('?')[0] // 获取到请求地址中的 method const method = req.method // 模拟获取信息的 url if(path === '/api/list' && method === 'GET') { // 命中该 url 后响应 res.end('响应的内容') } // 模拟发送信息的 url if(path === '/api/create' && method === 'POST') { // 命中该 url 后响应 res.end('响应的内容') } // 不命中 url 后的响应 res.end('404') }) server.listen(3000) // 可以监听 http 请求
什么是 querystring:
querystring 的作用:
const http = require('http') const server = http.createServer((req, res) => { // 获取到请求地址中的 url const url = req.url // 这一步是为了去掉 url 中的参数,从而获得真正的 url const path = url.split('?')[0] // 获取 querystring const queryStr = url.split('?')[1] // type=0 或 1 // 获取到请求地址中的 method const method = req.method // querystring 解析原理 const query = {} if(queryStr) { queryStr.split('&').forEach(item => { const key = item.split('=')[0] // type const val = item.split('=')[1] // 0 或 1 query[key] = val // {'type':'0'} }) } // 模拟获取信息的 url if(path === '/api/list' && method === 'GET') { if(query.type === '0') { // 当类型为 0 时执行的操作 res.end('响应的内容') } if(query.type === '1') { // 当类型为 1 时执行的操作 res.end('响应的内容') } } // 不命中 url 后的响应 res.end('404') }) server.listen(3000) // 可以监听 http 请求
直接使用 nodejs 自带的 querystring:
const http = require('http') // nodejs 自带的 querystring const querystring = require('querystring') const server = http.createServer((req, res) => { // 获取到请求地址中的 url const url = req.url // 这一步是为了去掉 url 中的参数,从而获得真正的 url const path = url.split('?')[0] // 获取到请求地址中的 method const method = req.method // 使用 nodejs 自带的 querystring 解析 const query = querystring.parse(queryStr || '') // 模拟获取信息的 url if(path === '/api/list' && method === 'GET') { if(query.type === '0') { // 当类型为 0 时执行的操作 res.end('响应的内容') } if(query.type === '1') { // 当类型为 1 时执行的操作 res.end('响应的内容') } } // 不命中 url 后的响应 res.end('404') }) server.listen(3000) // 可以监听 http 请求
url 的 hash 是否能起 querystring 同样的作用:
论:结构化与非结构化:
使用 res 设置返回状态码、Content-type、Body:
res.writeHead(200, { 'Content-type': 'application/json' })
如何返回 JSON 数据:
const http = require('http') // nodejs 自带的 querystring const querystring = require('querystring') const server = http.createServer((req, res) => { // 获取到请求地址中的 url const url = req.url // 这一步是为了去掉 url 中的参数,从而获得真正的 url const path = url.split('?')[0] // 获取到请求地址中的 method const method = req.method // 使用 nodejs 自带的 querystring 解析 const query = querystring.parse(queryStr || '') // 模拟获取信息的 url if(path === '/api/list' && method === 'GET') { const result = { errno: 0, data: [ { username: 'zll', content: 'content1'}, { username: 'lgk', content: 'content2'} ] } res.writeHead(200, { 'Content-type': 'application/json' }) // JSON.stringify(result) 将 JSON 转为 字符串形式 res.end(JSON.stringify(result)) } // 模拟发送信息的 url if(path === '/api/create' && method === 'POST') { const result = { errno: 0, message: '发送信息成功' } res.writeHead(200, { 'Content-type': 'application/json' }) // JSON.stringify(result) 将 JSON 转为 字符串形式 res.end(JSON.stringify(result)) } // 不命中 url 后的响应 res.writeHead(404, { 'Content-type': 'text/plain' }) res.end('404 Not Found') }) server.listen(3000) // 可以监听 http 请求
如何返回 html 数据:
// 示例
res.writeHead(404, { 'Content-type': 'text/html' })
res.end(`
<!DOCTYPE html>
<html>
<head>
<title>404</title>
</head>
<body>
<h1>404 Not Found</h1>
</body>
</html>
`)
流 stream 数据:
演示获取 Request Body:
// 模拟发送信息的 url if(path === '/api/create' && method === 'POST') { const reqType = req.headers['content-type'] let bodyStr = '' // req.on('data') 方法用于服务端去识别“流”,并接受数据 req.on('data', chunk => { // chunk 即“流”的每一段数据 // 拼接每一段流形式完整的数据 // 这里要使用 toString() 将每一段流转为字符串形式 bodyStr = bodyStr + chunk.toString() }) // 该方法可以使服务端知道数据传完了 req.on('end', () => { if(reqType === 'application/json') { // 将数据转为 JSON 格式 const body = JSON.parse(bodyStr) } res.end('接收完成') // 异步 }) return }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。