随笔分类 - Node.js
摘要:远程通信方式 通信方式: Stdio: 推荐,高效、简洁、本地 Streamable HTTP: 远程 前置知识 SSE 全称 Server-Sent Events,中文是“服务器发送事件”。是一种基于 HTTP 的单向通信协议,由浏览器发起连接,服务器可以持续不断地向客户端推送数据。 你可以把它想
阅读全文
摘要:chokidar chokidar 是一个功能强大、跨平台、性能优秀的 文件系统监听库,适用于 Node.js 环境,底层使用原生 fs.watch 和 fs.watchFile,并在 macOS/Linux 上优先使用更高效的 fsevents(若可用)。 基本用法: import chokida
阅读全文
摘要:SSE SSE(Server-Sent Events) 是一种浏览器原生支持的、单向的流式通信协议: 服务器 → 客户端 持续不断推送数据(比如:消息、token、进度) 它基于 HTTP 协议,使用 MIME 类型 text/event-stream。 核心特性 单向通信:只能从服务端推送到客户端
阅读全文
摘要:When you run node.js you can run node --expose-gc index.js Which will allow you manully cleanup gc: global.gc() Map For map, it is easy to cost memory
阅读全文
摘要:Using the Node.js glob function as a practical example, you'll learn how Array.fromAsync facilitates the creation of arrays from asynchronous iterable
阅读全文
摘要:You can create a javascript file as a bash file: #!/usr/bin/env node console.log("Hello World!") Run the script: ./script.js
阅读全文
摘要:Normally you need to put require("dotenv").config() console.log(process.env.TEST) to access env vars WIth --node-fileflag you don't need to do that an
阅读全文
摘要:# Create a node cli ## Init a project Run: `npm run init` Let's say we want to create a cli command call `note-dev`, let's add this into `package.json
阅读全文
摘要:logger.ts // .env LOGGER_LEVEL=debug // logger.ts import * as winston from "winston"; export const logger = winston.createLogger({ level: process.env.
阅读全文
摘要:import * as dotenv from "dotenv"; const result = dotenv.config(); if (result.error) { console.log('Error loading environment variables, aborting.') pr
阅读全文
摘要:https://www.npmjs.com/package/umzug // index.js const { Sequelize } = require('sequelize'); const { Umzug, SequelizeStorage } = require('umzug'); cons
阅读全文
摘要:We use needleas a client on Node.js express server for fetching the data or site. https://www.npmjs.com/package/needle The leanest and most handsome H
阅读全文
摘要:Express app: import cors from 'cors'; import express, { Application } from 'express'; import routes from './routes'; import * as middlewares from './m
阅读全文
摘要:Creating a CLI in Node.js just takes a extra step or two because they are really just an ordinary Node.js app wrapped behind a bin command. For this e
阅读全文
摘要:If you cannot catch those pesky errors for any reason. Maybe some lib is throwing them and you can't catch them. You can use: process.on('uncaughtExce
阅读全文
摘要:CrudController: 查看代码 export const getOne = model => async (req, res) => { try { const doc = model .findOne({ createdBy: req.user._id, _id: req.params.
阅读全文
摘要:Each model controller: import { crudControllers } from '../../utils/crud' import { Item } from './item.model' export default crudControllers(Item) You
阅读全文
摘要:Model: import mongoose from 'mongoose' const itemSchema = new mongoose.Schema( { name: { type: String, required: true, trim: true, maxlength: 50 }, st
阅读全文
摘要:Model: import mongoose from 'mongoose' const itemSchema = new mongoose.Schema( { name: { type: String, required: true, trim: true, maxlength: 50 }, st
阅读全文
摘要:Example 1: import mongoose from 'mongoose' const itemSchema = new mongoose.Schema( { name: { type: String, required: true, trim: true, maxlength: 50 }
阅读全文