# reactWithNode **Repository Path**: loveyingshi/reactWithNode ## Basic Information - **Project Name**: reactWithNode - **Description**: 使用react+node实现的项目 - **Primary Language**: JavaScript - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-05-21 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # AdvancedNodeStarter project with react and node 使用 github 账号进行登录 ## 启动 ``` npm run dev ``` ## mac 安装 redis 通过 brew 进行安装 redis ``` /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)" ``` 安装完之后 运行有可能报错 ``` brew services start redis // Could not connect to Redis at 127.0.0.1:6379: Connection refused ``` 原因是需要先开启服务端,这需要先配置—— 1.找到 redis.conf 并修改 daemonize no 为 daemonize yes ,这样就可以默认启动就后台运行 2.开启客户端要确保服务端启动 使用配置开启 redis 服务 ``` redis-server /usr/local/redis-4.0.10/redis.conf ``` ## cache.js ### 对 mongoose.Query.prototype 中对 exec 和 cache 方法进行重新定义 ``` // 如果在reids查到key 则返回 const cacheValue = await client.get(key); // 如果存在 if (cacheValue) { const doc = JSON.parse(cacheValue); return Array.isArray(doc) ? doc.map(d => new this.model(d)) : new this.model(doc); } // 否则 将查询的结果存在redis中 const result = await exec.apply(this, arguments); client.set(key, JSON.stringify(result)); ``` ### 设置缓存过期时间 ``` client.set(key, JSON.stringify(result), "EX", 10); ``` ## cleanCache.js 使用 middleware 自动清除缓存 ``` const { clearHash } = require("../services/cache"); module.exports = async (req, res, next) => { await next(); clearHash(req.user.id); }; ``` ## Automated Headless Browser Testing 使用 jest 进行自动化测试 ### puppeteer 启动 Chrome 实例并通过 javascript 与之交互 ``` browser = await puppeteer.launch({ headless: false }); page = await browser.newPage(); await page.goto("localhost:3000"); ``` ### 模拟用户点击按钮 ``` test("we click the login button", async () => { await page.click(".right a"); const url = await page.url(); console.log(url); expect(url).toMatch(/github\.com/); }); ``` ### oauth 的问题 第三方认证有时会认证是否真人操作 因此需要使用自动化测试解决身份认证问题 ### jest 中使用 mongoose 由于 jest 不会运行 service 端的组件, 只会找到.test 的文件 因此 mongoose.moel('user')会出错 - 在 package.json 中新增 ``` "jest": { "setupTestFrameworkScriptFile": "./tests/setup.js" }, ```