Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sesson20 #18

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions node/01/api/01-run.js

This file was deleted.

11 changes: 0 additions & 11 deletions node/01/api/02-useModule.js

This file was deleted.

30 changes: 21 additions & 9 deletions node/01/api/03-fs.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
const fs = require('fs')
// (async () => {
// const fs = require('fs')
// const { promisify } = require('util')
// const readFile = promisify(fs.readFile)
// const data = await readFile('./conf.js')
// console.log(data.toString())
// })()

// process.nextTick(async () => {
// })


// 同步读取
// const data = fs.readFileSync('./conf.js')
// console.log('data', data.toString())

// fs.readFile('./conf.js', (err, data) => {
// if (err) throw err
// console.log('data', data.toString())
// })


// 同步
// const data = fs.readFileSync('./download.js')
// console.log(data,data.toString())

// 异步方式
fs.readFile('./download.js',(err,data) => {
if(err) throw err
console.log(data)
})
14 changes: 6 additions & 8 deletions node/01/api/04-buffer.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
// 创建一个长度为10字节以0填充的Buffer
const buf1 = Buffer.alloc(10)
console.log(buf1)
console.log('buf1',buf1)

// 创建一个Buffer包含ascii.
const buf2 = Buffer.from('a')
console.log(buf2,buf2.toString())
console.log('buf2',buf2)

// 创建Buffer包含UTF-8字节
const buf3 = Buffer.from('中文')
console.log(buf3)
console.log('buf3',buf3,buf3.toString())

// 0/1 bit -> 8bit

// 合并Buffer
const buf4 = Buffer.concat([buf2,buf3])
console.log(buf4,buf4.toString())
console.log('buf4',buf4,buf4.toString())
55 changes: 39 additions & 16 deletions node/01/api/05-http.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,50 @@
const http = require('http')
const fs = require('fs')
const server = http.createServer((request, response) => {
// response.end('hello ...')
const { url, method ,headers} = request
if (url === '/' && method === 'GET'){
// 静态页面服务
fs.readFile('index.html',(err,data) => {
// console.log('this is a request',getPrototypeChain(request))
// response.end('Hello 666') // 1
// response = '666' // Koa
// 信达雅

const { url, method, headers } = request
if (url === '/' && method === 'GET') {
fs.readFile('index.html', (err, data) => {
if (err) {
response.writeHead(500, {
'Content-Type': 'text/plain;charset=utf-8'
})
response.end('500 服气')
return
}
response.statusCode = 200
response.setHeader('Content-Type','text/html')
response.end(data)
})
}else if(url === '/users' && method === 'GET'){
// Ajax服务
response.writeHead(200,{
'Content-Type': 'application/json'
}
else if (url === '/users' && method === 'GET') {
response.writeHead(200, {
'Content-Type' : 'application/json'
})
response.end(JSON.stringify({
name : 'laowang'
}))
}else if(method === 'GET' && headers.accept.indexOf('image/*') !== -1){
// 图片文件服务
fs.createReadStream('./'+url).pipe(response)
response.end(JSON.stringify([{name : 'tom'}]))
} else if (method === 'GET' && headers.accept.indexOf('image/*') !== -1) {
fs.createReadStream('.' + url).pipe(response) // ./1.png
}
else {
response.statusCode = 404
response.setHeader('Content-Type','text/plain;charset=utf-8')
response.end('404 页面没有 。。。')
}
})

function getPrototypeChain(obj) {
const protoChain = []
while (obj = Object.getPrototypeOf(obj)) {
protoChain.push(obj)
}
return protoChain
}


server.listen(3000, () => {
console.log('Server is start at 3000')
})
server.listen(3000)
5 changes: 2 additions & 3 deletions node/01/api/06-stream.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//二进制友好,图片操作
const fs = require('fs')
const rs = fs.createReadStream('./img.png')
const ws = fs.createWriteStream('./img2.png')
const rs = fs.createReadStream('./1.png')
const ws = fs.createWriteStream('./2.png')
rs.pipe(ws)
Binary file added node/01/api/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added node/01/api/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions node/01/api/conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
aaabb
23 changes: 0 additions & 23 deletions node/01/api/download.js

This file was deleted.

Binary file removed node/01/api/img.png
Binary file not shown.
Binary file removed node/01/api/img2.png
Binary file not shown.
5 changes: 2 additions & 3 deletions node/01/api/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>Hello ..</h1>
<img src="img.png" alt="">
<h1>Hello 666</h1>
<img src="1.png" alt="">
</body>
</html>
42 changes: 0 additions & 42 deletions node/01/promise/index.js

This file was deleted.

14 changes: 0 additions & 14 deletions node/01/promise/promisify.js

This file was deleted.

14 changes: 0 additions & 14 deletions node/01/spawn-cross-platform/__tests__/index.spec.js

This file was deleted.

33 changes: 0 additions & 33 deletions node/01/spawn-cross-platform/index.js

This file was deleted.

15 changes: 5 additions & 10 deletions node/01/vue-auto-router-cli/bin/kkb.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
#!/usr/bin/env node
const program = require('commander')
program.version(require('../package').version)
program
.command('init <name>')
program.version(require('../package.json').version)
program.command('init <name>')
.description('init project')
.action(
require('../lib/init')
)
.action(require('../lib/init'))

program
.command('refresh')
.description('refresh routers...')
.action(require('../lib/refresh'))
program
.command('serve')
.description('serve')
.action(require('../lib/serve'))

program.parse(process.argv)
6 changes: 3 additions & 3 deletions node/01/vue-auto-router-cli/lib/download.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const {promisify} = require('util')
module.exports.clone = async (repo,desc) => {
const { promisify } = require('util')
module.exports.clone = async function (repo, desc) {
const download = promisify(require('download-git-repo'))
const ora = require('ora')
const process = ora(`下载.....${repo}`)
const process = ora(`下载..... ${repo}`)
process.start()
await download(repo, desc)
process.succeed()
Expand Down
Loading