Mrcdh技术博客 Mrcdh技术博客
首页
  • Html5
  • Javascript
  • Nodejs
  • electron
  • Android
  • 微信公众号
  • 框架
  • 其他
  • Mysql
  • PHP
  • Python
  • java
  • Gulp
  • 其它
  • 网站
  • 资源
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Mrcdh

全栈开发小学生
首页
  • Html5
  • Javascript
  • Nodejs
  • electron
  • Android
  • 微信公众号
  • 框架
  • 其他
  • Mysql
  • PHP
  • Python
  • java
  • Gulp
  • 其它
  • 网站
  • 资源
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Html5

  • JavaScript

  • Nodejs

    • Nodejs 使用 nodemailer 发送邮件
    • npm镜像管理
    • npm&yarn&npx
    • puppeteer+axios保持登录请求
    • 使用node创建window服务启动程序
    • node图片转base64与base64转图片
    • node图片生成pdf
    • 常用npm库
      • http-server (HTTP 服务器)
        • 安装
        • 使用
      • cheerio (Node 版 Jquery)
        • 安装
        • 基本使用
      • sleep-monster(阻碍睡眠)
        • 安装
        • API
        • 案例
      • nodemon
      • chokidar(监听文件变化插件)
    • 使用pnpm的monorepo组织项目
    • npm&yarn&pnpm命令对比
    • Node常见问题
    • cheerio
    • request
    • selenium-webdriver
    • midway使用常见问题
    • dayjs的基本使用
    • ffmpeg的基本使用
    • mitt微型事件监听发布器
  • express

  • electron

  • Android

  • 微信公众号

  • 框架

  • 其他

  • selenium

  • Sequelize

  • 大前端
  • Nodejs
mrcdh
2021-02-07
目录

常用npm库

# http-server (HTTP 服务器)

http-server 是一个简单的零配置命令行 http 服务器。它对于生产使用来说足够强大,但它简单且易于破解,可用于测试、本地开发和学习。

# 安装

npm i -g http-server
1

# 使用

http-server [path] [options]
1
  • [path]:默认为当前项目的public文件夹,如果不存在则指定为当前根目录。 [options]参数说明 |参数名|说明| |---|---| |-p|端口号(默认为8080)| |-a|IP地址(默认为0.0.0.0)| |-d|显示目录列表(默认为true)| |-i|是否在访问目录地址时,默认显示以“index”命名的网页文件(默认为true)| |-g或--gzip|是否移动gzip(默认false)| |-e或--ext|默认显示文件的扩展名(默认html)| |-s或--silent|禁止日志信息输出| |--cors|启动cors通过Access-Control-Allow-Origin协议头| |-o [path]|启动服务后打开浏览器查看当前项目| |-c|通过cache-control max-age协议头设置缓存时间,单位秒。例如,“-c10”表示缓存10秒,默认3600秒,关闭缓存使用“-c-l”| |-U或--utc|在日志消息中使用UTC时间格式| |--log-ip|启用客服端地址日志| |--username|基于基本认证的用户名| |--password|基于基本认证的密码| |-P或--proxy|代理所有不存在的请求地址至指定网址,比如:-p https://mrcdh.cn (opens new window)| |-S,--ssl,--tls|启动tls/ssl(https)| |-C或--cert|ssl证书文件地址(默认为cert.pem)| |-K或--key|ssl秘钥文件地址(默认key.pem)| |-r或--robots|提供一份爬虫协议(默认为所有收入,即“User-agent:* Disallow:/”)| |-h或--help|打印帮助说明并退出|

插件详情查看:http-server (opens new window)

# cheerio (Node 版 Jquery)

为服务器特别定制的,快速、灵活、实施的jQuery核心实现。
cheerio 和 jQuery 选择器的实现几乎是相同的,所以 API 非常相似。
详细使用文档

# 安装

npm install cheerio
1

# 基本使用

const cheerio = require("cheerio");
const $ = cheerio.load('<h2 class="title">Hello world</h2>');

$("h2.title").text("Hello there!");
$("h2").addClass("welcome");

$.html();
1
2
3
4
5
6
7

# sleep-monster(阻碍睡眠)

# 安装

npm install sleep-monster
1

# API

异步函数(非阻塞线程):

  • asyncSleep 单位:毫秒,常用
  • asyncSleepSeconds 单位:秒
  • asyncSleepMinutes 单位:分钟
  • asyncSleepHours 单位:小时
  • asyncSleepDays 单位:天
  • asyncSleepMonths 单位:月
  • asyncSleepYears 单位:年

同步函数(阻塞线程):

  • syncSleep 单位:毫秒,常用
  • syncSleepSeconds 单位:秒
  • syncSleepMinutes 单位:分钟
  • syncSleepHours 单位:小时
  • syncSleepDays 单位:天
  • syncSleepMonths 单位:月
  • syncSleepYears 单位:年

# 案例

import { asyncSleep } from "sleep-monster";

const test = async () => {
  console.log("go to sleep...");
  const res = await asyncSleep(2000);
  /* sleep之后需要执行的代码 */
  console.log(res); // true
  console.log("2000ms later now");
};
test();

console.log("The synchronized code is still executed"); // 无法阻塞其作用域之外的代码执行

const test = () => {
  console.log("go to sleep...");
  asyncSleep(2000).then((res) => {
    /* sleep之后需要执行的代码 */
    console.log(res); // true
    console.log("2000ms later now");
  });
};
test();

console.log("The synchronized code is still executed"); // 无法阻塞其作用域之外的代码执行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

同步的方式

import { syncSleep } from "sleep-monster";

setTimeout(() => {
  console.log("3000ms"); // 异步任务也被阻塞
}, 3000);

syncSleep(5000);

console.log(
  "5000ms later now.Both synchronous and asynchronous code are blocked."
); // 所有的同步任务和异步任务都被阻塞了
1
2
3
4
5
6
7
8
9
10
11

# nodemon

nodemon 是一个帮助开发基于 node.js 的应用程序的工具,它在检测到目录中的文件变化时自动重启 node 应用程序。

nodemon 不需要对您的代码或开发方法进行任何额外的更改。nodemon 是节点的替换包装。要使用 nodemon,请在执行脚本时替换命令行中的 node 一词。

npm i -g nodemon
npm i --save-dev nodemon
1
2

# chokidar(监听文件变化插件)

#node
上次更新: 2023/09/22, 16:54:32
node图片生成pdf
使用pnpm的monorepo组织项目

← node图片生成pdf 使用pnpm的monorepo组织项目→

最近更新
01
uniapp常见问题
03-19
02
Vue3项目搭建
12-25
03
使用pnpm的monorepo组织项目
11-01
更多文章>
Theme by Vdoing | Copyright © 2020-2025 Mrcdh | 苏ICP备2020058908号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×