Skip to content

Commit

Permalink
Version 2.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jason5ng32 committed Dec 13, 2023
1 parent 352495f commit d5343cf
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
12 changes: 11 additions & 1 deletion api/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,17 @@ module.exports = (req, res) => {
return res.status(403).json({ error: 'No referer header' });
}

const { latitude, longitude, language } = req.query;
//妈的,兼容本地和 Vercel 的环境真烦
let { latitude, longitude, language } = req.params;
if (!latitude || !longitude || !language) {
latitude = req.query.latitude;
longitude = req.query.longitude;
language = req.query.language;
}
if (!latitude || !longitude || !language) {
return res.status(400).json({ error: 'Missing latitude, longitude, or language' });
}

const mapSize = '800,640';
const pp = `${latitude},${longitude};66`;
const fmt = 'jpeg';
Expand Down
4 changes: 2 additions & 2 deletions public/res/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ new Vue({
: false;
const mapUrl =
data.latitude && data.longitude
? `/api/map/${data.latitude},${data.longitude}/${this.bingMapLanguage}`
? `/api/map?latitude=${data.latitude}&longitude=${data.longitude}&language=${this.bingMapLanguage}`
: "";

// 更新卡片数据
Expand Down Expand Up @@ -256,7 +256,7 @@ new Vue({
: false,
mapUrl:
data.lat && data.lon
? `/api/map/${data.lat},${data.lon}/${this.bingMapLanguage}`
? `/api/map?latitude=${data.lat}&longitude=${data.lon}&language=${this.bingMapLanguage}`
: "",
};

Expand Down
11 changes: 3 additions & 8 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
require('dotenv').config();
// Vercel 上不需要 express,因为 Vercel 会自动处理
const express = require('express');
const path = require('path');
const mapHandler = require('./api/map');
const validateMapKeyHandler = require('./api/validate-map-key');

const app = express();
const port = process.env.PORT || 8966;
app.get('/api/map/:latitude,:longitude/:language', mapHandler);

// 使用查询参数处理所有地图请求
app.get('/api/map', mapHandler);

// 设置静态文件服务
app.use(express.static(path.join(__dirname, 'public')));

// 模拟 Vercel 的无服务器函数
app.all('/api/map', (req, res) => {
req.query = { ...req.params, ...req.query }; // 合并 params 和 query
mapHandler(req, res);
});

app.all('/api/validate-map-key', validateMapKeyHandler);

// 启动服务器
Expand Down

0 comments on commit d5343cf

Please sign in to comment.