koa (http 【301】、http【304】 、實現靜態文件服務狀態對應示例。

目的爲了,學習http狀態碼
github L6zt 本身原創
git 地址 html

  1. git clone https://github.com/L6zt/httpS...
  2. npm install
  3. node index
  4. 看控制檯
/* 
  borhter, 看着 http 說明 對應的響應頭
  boreher, 看着 node koa 文檔進行對應操做
  我很菜
*/
const koa = require('koa')
const $path = require('path')
const fs = require('fs');
const mime = require('mime');
const app = new koa();
let defaultPort = 8080
    // 返回文件內容 buffer
const findAndReturnFile = async(fileName) => {
        return await new Promise((r, j) => {
            fs.readFile(fileName,
                // {encoding: 'utf-8'}, 
                (err, data) => {
                    if (err) {
                        return j(err)
                    } else {
                        r(data)
                    }
                })
        })
    }
    // 獲取 gmt 時間戳
const getUtmTimestamp = (date) => new Date(date).toUTCString()
    // 獲取文件描述
const findAndReturnFileDesc = async(fileName) => {
    const fd = await new Promise((r, j) => {
        fs.open(fileName, 'r', (err, fd) => {
            if (err) {
                return j(err)
            } else {
                r(fd)
            }
        })
    })
    const stat = await new Promise((r, j) => {
        fs.fstat(fd, (err, stat) => {
            if (err) {
                return j(err)
            }
            r(stat)
            fs.close(fd, (err) => {
                if (err) j(err)
            })
        })
    })
    return stat
}
app.use(async(ctx, next) => {
    await next()
    console.log('do you have little grils?');
})

app.use(async(ctx) => {
    const { response } = ctx;
    let { path } = ctx
    // 事例 模擬重定向 
    if (/^\/301*?/.test(path)) {
        ctx.response.status = 301
        ctx.response.set({
            // 重定向地址
            Location: 'https://baidu.com'
        })
        ctx.body = '耐心等待'
        return
    }
    // 200 --- 模擬koa的靜態服務
    if (/assets\/.*/.test(path)) {
        path = path.replace(/^assets\//, '')
        const filePath = $path.join(__dirname, path)
        const fileMime = mime.getType(path.split('.').pop())
        ctx.status = 200
        const { mtime } = await findAndReturnFileDesc(filePath)
        const result = await findAndReturnFile(filePath)
        const { 'if-modified-since': ifModifiedSinceDate } = ctx.headers
        const LastModifiedDate = getUtmTimestamp(mtime);
        // 304
        if (LastModifiedDate === ifModifiedSinceDate) {
            ctx.status = 304
            return
        }
        ctx.set({
            'Content-Type': fileMime,
            'Xserver': 'xBykoa',
            'Last-Modified': LastModifiedDate
        })
        ctx.body = result;
    }
})
const startKoadApp = (port) => new Promise((r, j) => app.listen(port, (err) => {
    if (err) {
        j(prot + 1)
    } else {
        const prefix = `http://localhost:${defaultPort}`
        console.log('***-服務器-***');
        console.log(`${prefix}/301`, '感覺301吧')
        console.log(`${prefix}/assets/**.ext`, '感覺獲取靜態資源的服務吧');
        console.log(`${prefix}/assets/index.html`);
        console.log('***--***');
    }
}));
startKoadApp(defaultPort).catch(e => {
    defaultPort++
    startKoadApp(defaultPort)
})
相關文章
相關標籤/搜索