node的自动化测试
# node 的自动化测试 Node 有有一个内置的测试库 node:test 测试执行命令: npm script test 编写测试脚本让命令更简洁: "scripts": { "start": "node index.js", "dev": "nodemon index.js", "test": "node --test" // 添加这个命令 },并创建一个 .test.js 文件,内容类似于: const...
more...重构独立模块
# 这是针对 Node.js 后端项目的工具类模块 # 让我们先总结 目录结构: /project-root |-- controllers | |-- blogs.js |-- models | |-- blog.js |-- node_modules |-- requests | |-- create_note.rest |-- utils | |-- config.js | |-- logger.js | |-- middleware.js |-- .env |-- .gitignore |-- app.js |-- index.js |-- package-lock.json |--...
more...Node.js和Express
# 准备 # 安装 node # 初始化 创建一个新的模板 npm init进入到 json 文件,对 scripts 进行改动 "scripts": { "start": "node index.js", // 添加 "test": "echo \"Error: no test specified\" && exit 1" },在根目录创建 index.js...
more...openweather天气获取
# 注册并获取 API Key 访问 https://openweathermap.org/api 注册账号。记得在邮箱验证,生成一个新的 API 密钥 # 通过 axios, async/await 获取天气信息 替换 YOUR_API_KEY 为 API 密钥 在 fetchWeather 函数中,我们使用 axios.get() 方法发送一个 HTTP GET 请求到 OpenWeatherMap API,并传递了 API 密钥和查询参数。 天气数据被存储在 weather 状态变量中。 capital 是城市名 const fetchWeather = async (capital)...
more...