中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁(yè) > news >正文

微信管理中心seo人員的職責(zé)

微信管理中心,seo人員的職責(zé),釘釘創(chuàng)建企業(yè),建設(shè)雅馬哈網(wǎng)站準(zhǔn)備工作 上一節(jié)實(shí)現(xiàn)了通過(guò) commander 的配置獲取到用戶的參數(shù),下面完成借用 promise 寫(xiě)成類(lèi)的方法一節(jié)沒(méi)有完成的任務(wù),實(shí)現(xiàn)一個(gè) http-server,https://www.npmjs.com/package/http-server,http-server 是一個(gè)簡(jiǎn)單的零配置命令行靜…

準(zhǔn)備工作

上一節(jié)實(shí)現(xiàn)了通過(guò) commander 的配置獲取到用戶的參數(shù),下面完成借用 promise 寫(xiě)成類(lèi)的方法一節(jié)沒(méi)有完成的任務(wù),實(shí)現(xiàn)一個(gè) http-server,https://www.npmjs.com/package/http-server,http-server 是一個(gè)簡(jiǎn)單的零配置命令行靜態(tài) HTTP 服務(wù)器。

需要用到的核心模塊

const http = require("http");
const path = require("path");
const url = require("url");
const fs = require("fs").promises;
const { createReadStream, createWriteStream, readFileSync } = require("fs");

需要用到的第三方模塊:

  • ejs 用來(lái)進(jìn)行模板渲染
  • mime 用來(lái)根據(jù)文件擴(kuò)展名獲取 MIME type, 也可以根據(jù) MIME type 獲取擴(kuò)展名。
  • chalk 用來(lái)控制臺(tái)輸出著色
  • debug 可以根據(jù)環(huán)境變量來(lái)進(jìn)行打印
const ejs = require("ejs"); // 服務(wù)端讀取目錄進(jìn)行渲染
const mime = require("mime");
const chalk = require("chalk");
const debug = require("debug")("server");

安裝依賴(lài)

npm install ejs@3.1.3 debug@4.1.1 mime@2.4.6 chalk@4.1.0

在這里插入圖片描述

配置環(huán)境變量

const debug = require("debug")("server");
// 根據(jù)環(huán)境變量來(lái)進(jìn)行打印 process.env.EDBUG
debug("hello kaimo-http-server");
set DEBUG=server
kaimo-http-server

在這里插入圖片描述

代碼實(shí)現(xiàn)

  1. 將拿到的配置數(shù)據(jù)開(kāi)啟一個(gè) server

www.js 里面實(shí)現(xiàn)

#! /usr/bin/env nodeconst program = require("commander");
const { version } = require("../package.json");
const config = require("./config.js");
const Server = require("../src/server.js");program.name("kaimo-http-server");
program.usage("[args]");
program.version(version);Object.values(config).forEach((val) => {if (val.option) {program.option(val.option, val.description);}
});program.on("--help", () => {console.log("\r\nExamples:");Object.values(config).forEach((val) => {if (val.usage) {console.log("  " + val.usage);}});
});// 解析用戶的參數(shù)
let parseObj = program.parse(process.argv);let keys = Object.keys(config);// 最終用戶拿到的數(shù)據(jù)
let resultConfig = {};
keys.forEach((key) => {resultConfig[key] = parseObj[key] || config[key].default;
});// 將拿到的配置數(shù)據(jù)開(kāi)啟一個(gè) server
console.log("resultConfig---->", resultConfig);
let server = new Server(resultConfig);
server.start();
  1. 實(shí)現(xiàn) server.js,主要的邏輯就是通過(guò)核心模塊以及第三方模塊將用戶輸入的參數(shù),實(shí)現(xiàn)一個(gè) Server 類(lèi),里面通過(guò) start 方法開(kāi)啟服務(wù),核心邏輯實(shí)現(xiàn)通過(guò)路徑找到這個(gè)文件返回,如果是文件夾處理是否有 index.html 文件,沒(méi)有的話就通過(guò)模板渲染文件夾里目錄信息。
// 核心模塊
const http = require("http");
const path = require("path");
const url = require("url");
const fs = require("fs").promises;
const { createReadStream, createWriteStream, readFileSync } = require("fs");// 第三方模塊
const ejs = require("ejs"); // 服務(wù)端讀取目錄進(jìn)行渲染
const mime = require("mime");
const chalk = require("chalk");
const debug = require("debug")("server");
// 根據(jù)環(huán)境變量來(lái)進(jìn)行打印 process.env.EDBUG
debug("hello kaimo-http-server");// 同步讀取模板
const template = readFileSync(path.resolve(__dirname, "template.ejs"), "utf-8");class Server {constructor(config) {this.host = config.host;this.port = config.port;this.directory = config.directory;this.template = template;}async handleRequest(req, res) {let { pathname } = url.parse(req.url);// 需要對(duì) pathname 進(jìn)行一次轉(zhuǎn)義,避免訪問(wèn)中文名稱(chēng)文件找不到問(wèn)題console.log(pathname);pathname = decodeURIComponent(pathname);console.log(pathname);// 通過(guò)路徑找到這個(gè)文件返回let filePath = path.join(this.directory, pathname);console.log(filePath);try {// 用流讀取文件let statObj = await fs.stat(filePath);// 判斷是否是文件if (statObj.isFile()) {this.sendFile(req, res, filePath, statObj);} else {// 文件夾的話就先嘗試找找 index.htmllet concatFilePath = path.join(filePath, "index.html");try {let statObj = await fs.stat(concatFilePath);this.sendFile(req, res, concatFilePath, statObj);} catch (e) {// index.html 不存在就列出目錄this.showList(req, res, filePath, statObj, pathname);}}} catch (e) {this.sendError(req, res, e);}}// 列出目錄async showList(req, res, filePath, statObj, pathname) {// 讀取目錄包含的信息let dirs = await fs.readdir(filePath);console.log(dirs, "-------------dirs----------");try {let parseObj = dirs.map((item) => ({dir: item,href: path.join(pathname, item) // url路徑拼接自己的路徑}));// 渲染列表:這里采用異步渲染let templateStr = await ejs.render(this.template, { dirs: parseObj }, { async: true });console.log(templateStr, "-------------templateStr----------");res.setHeader("Content-type", "text/html;charset=utf-8");res.end(templateStr);} catch (e) {this.sendError(req, res, e);}}// 讀取文件返回sendFile(req, res, filePath, statObj) {// 設(shè)置類(lèi)型res.setHeader("Content-type", mime.getType(filePath) + ";charset=utf-8");// 讀取文件進(jìn)行響應(yīng)createReadStream(filePath).pipe(res);}// 專(zhuān)門(mén)處理錯(cuò)誤信息sendError(req, res, e) {debug(e);res.statusCode = 404;res.end("Not Found");}start() {const server = http.createServer(this.handleRequest.bind(this));server.listen(this.port, this.host, () => {console.log(chalk.yellow(`Starting up kaimo-http-server, serving ./${this.directory.split("\\").pop()}\r\n`));console.log(chalk.green(`       http://${this.host}:${this.port}`));});}
}module.exports = Server;
  1. 模板 template.ejs 的代碼
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>列表模板</title></head><body><% dirs.forEach(item=> { %><li><a href="<%=item.href%>"><%=item.dir%></a></li><% }) %></body>
</html>

實(shí)現(xiàn)效果

控制臺(tái)我們輸入下面命令啟動(dòng)一個(gè)端口為 4000 的服務(wù)

kaimo-http-server --port 4000

我們?cè)L問(wèn) http://localhost:4000/,可以看到

在這里插入圖片描述

我們?cè)谶M(jìn)入 public 文件,里面有 index.html 文件

在這里插入圖片描述

點(diǎn)擊 public 目錄進(jìn)入 http://localhost:4000/public,可以看到返回了 index.html 頁(yè)面

在這里插入圖片描述

我們?cè)谶M(jìn)入 public2 文件,里面沒(méi)有 index.html 文件

在這里插入圖片描述

點(diǎn)擊 public2 目錄進(jìn)入 http://localhost:4000/public2,看到的就是列表

在這里插入圖片描述

我們可以點(diǎn)擊任何的文件查看:比如 凱小默.txt

在這里插入圖片描述

http://www.risenshineclean.com/news/64620.html

相關(guān)文章:

  • aspcms網(wǎng)站模板網(wǎng)絡(luò)推廣公司有多少家
  • 中英西班牙網(wǎng)站建設(shè)一鍵優(yōu)化是什么意思
  • 浙江臺(tái)州做網(wǎng)站的公司有哪些網(wǎng)絡(luò)推廣網(wǎng)絡(luò)營(yíng)銷(xiāo)外包
  • 廈門(mén)網(wǎng)站建設(shè)哪家強(qiáng)農(nóng)產(chǎn)品網(wǎng)絡(luò)營(yíng)銷(xiāo)
  • 做網(wǎng)站軟件frontpage百度排名點(diǎn)擊軟件
  • 織夢(mèng)移動(dòng)網(wǎng)站百度站長(zhǎng)社區(qū)
  • 遼陽(yáng)好的網(wǎng)站建設(shè)公司百度競(jìng)價(jià)推廣流程
  • 梧州網(wǎng)站設(shè)計(jì)理念網(wǎng)絡(luò)seo外包
  • 贛州網(wǎng)站建設(shè)費(fèi)用百度seo培訓(xùn)要多少錢(qián)
  • 提供網(wǎng)站建設(shè)費(fèi)用企業(yè)網(wǎng)站優(yōu)化技巧
  • 做軟件常用的網(wǎng)站有哪些軟件有哪些事件營(yíng)銷(xiāo)案例
  • 移動(dòng)網(wǎng)站建站視頻網(wǎng)絡(luò)推廣的基本方法有哪些
  • 做平面什么網(wǎng)站好用今日熱搜前十名
  • 個(gè)人做旅游網(wǎng)站的意義百度首頁(yè)排名優(yōu)化服務(wù)
  • 做網(wǎng)站用什么語(yǔ)言好廣告聯(lián)盟app下載
  • 南昌新建網(wǎng)站建設(shè)如何讓百度快速收錄網(wǎng)站文章
  • 做網(wǎng)絡(luò)推廣的網(wǎng)站有哪些如何做電商 個(gè)人
  • 包頭正規(guī)旅游網(wǎng)站開(kāi)發(fā)哪家好aso關(guān)鍵詞排名優(yōu)化是什么
  • 做網(wǎng)站界面尺寸是多少網(wǎng)上營(yíng)銷(xiāo)的平臺(tái)有哪些
  • wordpress sparklingseo視頻教程百度云
  • 南京房產(chǎn)網(wǎng)站建設(shè)手機(jī)如何做網(wǎng)站
  • 保定市做網(wǎng)站的公司網(wǎng)絡(luò)策劃營(yíng)銷(xiāo)
  • wordpress怎么切換成中文的seo服務(wù)顧問(wèn)
  • 網(wǎng)站公司不給ftp怎么免費(fèi)制作網(wǎng)頁(yè)
  • 做網(wǎng)站是什么優(yōu)化設(shè)計(jì)官方電子版
  • 網(wǎng)站與域名的關(guān)系廣告免費(fèi)發(fā)布信息平臺(tái)
  • 綿陽(yáng)政府網(wǎng)站建設(shè)國(guó)內(nèi)建站平臺(tái)有哪些
  • 動(dòng)漫谷網(wǎng)站建設(shè)策劃書(shū)網(wǎng)站推廣的平臺(tái)
  • 太平洋在線企業(yè)建站系統(tǒng)華聯(lián)股份股票
  • 做類(lèi)似3d溜溜的網(wǎng)站網(wǎng)上怎么注冊(cè)公司免費(fèi)的