跳到主要内容

在 next.js 中使用 https

· 阅读需 1 分钟
1adybug
子虚伊人
import { readFileSync } from "fs"
import { createServer } from "https"
import { join } from "path"

import next from "next"

const app = next({})
const handle = app.getRequestHandler()

// https 证书相关
const key = readFileSync(join("/etc/letsencrypt/live", "yourdomain.com", "privkey.pem"), "utf8")

const cert = readFileSync(join("/etc/letsencrypt/live", "yourdomain.com", "cert.pem"), "utf8")
const ca = readFileSync(join("/etc/letsencrypt/live", "yourdomain.com", "chain.pem"), "utf8")

app.prepare().then(() => {
createServer({ key, cert, ca }, (req, res) => {
handle(req, res)
}).listen(3000)
})

打印长时间运行命令的输入

· 阅读需 3 分钟
1adybug
子虚伊人

如果你想要实时打印长时间运行命令的输出,比如监控某个过程的日志输出,你应该使用 child_process 中的 spawn 来代替 exec,因为 spawn 提供了一个流式接口,可以让你实时接收数据。下面是一个使用 spawn 实时打印输出的示例:

import { spawn } from "child_process"

const child = spawn("node -v", { shell: true })

child.stdout.on("data", data => {
console.log(`stdout: ${data}`)
})

child.stderr.on("data", data => {
console.error(`stderr: ${data}`)
})

child.on("close", code => {
console.log(`child process exited with code ${code}`)
})

这段代码会实时打印出命令的标准输出和标准错误输出。使用 spawn 时,你可以通过监听 data 事件来实时获取命令的输出。这对于需要实时监控输出的场景非常有用。

记得替换 your_long_running_command_here 为你想要执行的实际命令。

在命令行中执行的许多命令会使用特殊的 ANSI 转义序列来改变输出的字体颜色或样式,从而使输出更易于阅读。默认情况下,当这些命令通过 Node.jsspawn 函数执行时,它们可能检测到自己不是在一个交互式终端环境中运行,因此可能不会输出这些颜色或样式。

要使 spawn 打印的输出效果与直接在命令行中执行的效果一样(包括字体颜色),你可以尝试以下几种方法:

  1. 强制命令以颜色输出

    一些命令提供了参数或环境变量来强制启用颜色输出,即使输出被重定向。例如,很多命令支持 --color 选项:

    import { spawn } from "child_process"

    const child = spawn("some-command", ["--color", "always"])

    对于那些不支持此类选项的命令,可能需要查看该命令的文档,看是否有其他方法可以强制启用颜色输出。

  2. 使用 shell 选项

    通过在 spawn 中设置 shell: true 选项,可以在一个 shell 环境中执行命令。这样做可能会鼓励一些命令输出颜色,因为它们“认为”自己是在一个终端中运行:

    import { spawn } from "child_process"

    const child = spawn("some-command", ["arg1", "arg2"], { shell: true })
  3. 保持输出到进程的标准输出

    如果你只是想要在 Node.js 脚本中直接查看输出(包括颜色),而不是处理输出数据,你可以将子进程的 stdoutstderr 直接连接到主进程的 stdoutstderr

    import { spawn } from "child_process"

    const child = spawn("some-command", ["arg1", "arg2"], { stdio: "inherit" })

    使用 stdio: "inherit" 选项,子进程的输出会直接显示在终端中,包括所有的颜色和格式化。

    示例 假设你想要使用 spawn 执行 git status 命令,并希望输出包含颜色:

    import { spawn } from "child_process"

    // 注意:这里使用shell: true来确保命令在shell中执行,可能有助于保留颜色输出
    const child = spawn("git", ["status", "--color=always"], {
    shell: true,
    stdio: "inherit",
    })

    child.on("exit", function (code, signal) {
    console.log(`子进程退出,退出码 ${code}`)
    })

    这个例子中,--color=always 告诉 git status 命令总是使用颜色输出,shell: true 确保在一个 shell 环境中执行命令,stdio: "inherit" 使得命令的输出直接显示在终端中,包括颜色。

注意

使用 shell: true 可能会增加安全风险,特别是当命令的参数来自不可信的源时。因此,只有在确实需要时才使用这个选项,并确保对输入进行适当的清理和验证。

命名规范

· 阅读需 1 分钟
1adybug
子虚伊人
  1. 不要使用歧义或者项目外读者不熟悉的缩写
  2. 尽可能给出具有描述性的词汇
  3. 文件名建议小写短横线命名法
  4. 对于包名使用驼峰命名法,并和默认导出的名称保持一致
  5. 类和接口类型使用大驼峰命名法,其中类名通常是名词或者名词短语
  6. 组件名和类的规范一致
  7. 接口名可以是形容词或者形容词短语
  8. 方法是用小驼峰命名法,通常是动词或者动词短语
  9. 布尔值可以使用 is 或者 has
  10. 对于私有属性和方法在变量名前加 #
  11. 对于常量名,使用全大写字母,并且单词之间用下划线分割
  12. 非常量名使用小驼峰命名法
  13. 参数名同非常量名
  14. 即使是缩写的单词也当做一般单词,首字母大写
  15. 如果单词已经有常规的驼峰写法则保留
  16. 钩子函数命名统一,如全部使用完成时或者 after

前端面试题

· 阅读需 1 分钟
1adybug
子虚伊人
  1. JavaScript 中有哪些数据类型

    stringnumberbooleanbigIntnullundefinedsymbolobject

  2. 谈一谈 requestAnimationFrame

  3. 说说数组在最近几年新增了哪些方法

  4. 如何在 canvas 中画一条直线

  5. 如何禁止用户选中文字

  6. vue 2 和 vue 3 分别是如何对数组实现响应式的

  7. vue 3 中 watch 函数是如何自动收集依赖的

  8. TypeScript 中有哪些自带的泛型,分别有什么作用

  9. 谈一谈 TypeScript 中的 extends 关键字

  10. TypeScript 中如何指定函数中的 this 的类型

有用的网站

· 阅读需 1 分钟
1adybug
子虚伊人
  • Visx 是一个基于 d3 的低阶 react 可视化原子库,主要是各类图表
  • CSS Generators 各种类型的 CSS 效果,比如绶带等,纯 CSS 实现
  • HTMLrev 各种类型的网站模版,免费可商用
  • tsParticles 各种粒子特效的网站

坐标转换

· 阅读需 8 分钟
1adybug
子虚伊人
坐标系名称产品说明
WGS84大地坐标系谷歌地图(国外)目前广泛使用的 GPS 全球卫星定位系统使用的坐标系
GCJ02火星坐标系高德地图、腾讯地图、谷歌地图(国内)中国国家测绘局制定的地理坐标系统,是由 WGS84 加密后得到的坐标系
BD09百度坐标系百度地图在 GCJ02 坐标系基础上再次加密。其中 BD09LL 表示百度经纬度坐标,BD09MC 表示百度墨卡托米制坐标

在 Express 中使用 https 证书

· 阅读需 1 分钟
1adybug
子虚伊人
import { readFileSync } from "fs"
import https from "https"

import express from "express"

const app = express()

app.get("/", async (req, res) => {
res.send("Hello, World!")
})

https
.createServer(
{
key: readFileSync("../root/.acme.sh/a.deep-sea.dynv6.net_ecc/a.deep-sea.dynv6.net.key"),
cert: readFileSync("../root/.acme.sh/a.deep-sea.dynv6.net_ecc/fullchain.cer"),
},
app,
)
.listen(8080)

ReadAbleStream 转换为 ReadStream

· 阅读需 1 分钟
1adybug
子虚伊人
import { Readable } from "stream"

const reader = readAbleStream.getReader()

const readStream = new Readable({
read() {
reader.read().then(({ done, value }) => {
if (done) readStream.push(null)
else readStream.push(value)
})
},
})

async function* nodeStreamToIterator(stream: ReadStream): AsyncGenerator<Buffer, void, never> {
for await (const chunk of stream) yield chunk
}

function iteratorToStream(iterator: AsyncGenerator<Buffer, void, never>): ReadableStream {
return new ReadableStream({
async pull(controller) {
const { value, done } = await iterator.next()

if (done) controller.close()
else controller.enqueue(value)
},
})
}
提示

2024 年 3 月 29 日更新

Node.js 中其实自带了转换的功能

import { Readable } from "stream"

async function main() {
const response = await fetch("http://example.com")
const readable = Readable.fromWeb(response.body as any)
}

main()