Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

将 mock 数据托管到本地,建立主备数据请求方案 #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 45 additions & 8 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ const express = require('express')
const ejs = require('ejs')
const path = require('path')
const axios = require('axios')
const fs = require('fs')

const PORT = 3001 //设置 express 服务器端口

/**
* 修改 axios 请求资源 baseUrl
* 请求本地资源时,如果不设置,将请求 80 端口
* 请求远程资源(包含完整的主机、端口、协议的资源路径)不会遇到这个问题
* 此处设置端口和 express 服务器端口一致
*/
axios.defaults.baseURL = 'http://localhost:' + PORT

const app = express()

Expand All @@ -14,42 +25,68 @@ app.set('view engine', 'ejs');
* Home Page & Post List Page
*/
app.get('/', async (req, res) => {
const response = await axios.get('https://raw.githubusercontent.com/getgridea/mock-json/master/list.json')
const response = await requestMockData('https://raw.githubusercontent.com/getgridea/mock-json/master/list.json')
res.render('index', { ...response.data })
})

/**
* Post Page
*/
app.get('/post/:postName', async (req, res) => {
const response = await axios.get('https://raw.githubusercontent.com/getgridea/mock-json/master/post.json')
const response = await requestMockData('https://raw.githubusercontent.com/getgridea/mock-json/master/post.json')
res.render('post', { ...response.data })
})

/**
* Archives Page
*/
app.get('/archives', async (req, res) => {
const response = await axios.get('https://raw.githubusercontent.com/getgridea/mock-json/master/archives.json')
const response = await requestMockData('https://raw.githubusercontent.com/getgridea/mock-json/master/archives.json')
res.render('archives', { ...response.data })
})

/**
* tags Page
*/
app.get('/tags', async (req, res) => {
const response = await axios.get('https://raw.githubusercontent.com/getgridea/mock-json/master/tags.json')
const response = await requestMockData('https://raw.githubusercontent.com/getgridea/mock-json/master/tags.json')
res.render('tags', { ...response.data })
})

/**
* tag Page
*/
app.get('/tag/:tagName', async (req, res) => {
const response = await axios.get('https://raw.githubusercontent.com/getgridea/mock-json/master/tag.json')
const response = await requestMockData('https://raw.githubusercontent.com/getgridea/mock-json/master/tag.json')
res.render('tag', { ...response.data })
})

//使用8080端口
app.listen(3001)
console.log("The server is running on 3001")
/**
* 请求 mock 数据
*/
app.get('/mock/:path', async (req, res) => {
const path = req.params.path;
fs.readFile(path.join(__dirname, 'mock', path), (err, json) => {
if (err) {
res.status(404).send(err);
return;
}
res.set('Content-Type', 'application/json').status(200).send(json);
})
})

//使用 PORT 端口
app.listen(PORT)
console.log("The server is running on " + PORT)

/**
* 请求 mock 主备数据
*/
function requestMockData(url) {
const backupUrl = '/mock/' + url.split('/').pop()
return axios.get(url)
.then(result => result)
.catch(reason => console.log(`[Main][${url}]\n${reason.message}`))
.then(() => axios.get(backupUrl))
.catch(reason => console.log(`[Backup][${url}]\n${reason.message}`))
}
299 changes: 299 additions & 0 deletions mock/archives.json

Large diffs are not rendered by default.

299 changes: 299 additions & 0 deletions mock/list.json

Large diffs are not rendered by default.

256 changes: 256 additions & 0 deletions mock/post.json

Large diffs are not rendered by default.

242 changes: 242 additions & 0 deletions mock/tag.json

Large diffs are not rendered by default.

238 changes: 238 additions & 0 deletions mock/tags.json

Large diffs are not rendered by default.