-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathapp 修改为两个路由.js
202 lines (163 loc) · 6.54 KB
/
app 修改为两个路由.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
const bodyParser = require('body-parser');
const axios = require('axios');
const express = require('express');
const app = express();
//设置一个数据库属性
const pgp = require('pg-promise')();
const dbConfig = {
host: 'localhost',
port: 5434,
database: 'postgis_34_sample',
user: 'postgres',
password: '12345678'
};
const db = pgp(dbConfig);
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/', (req, res) => {
res.render('index');
});
//在app.js中设置public文件夹为静态文件夹
app.use(express.static('public'));
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
app.post('/place', async (req, res) => {
const placeName = req.body.placeName;
// 检查place参数是否有效
if (!placeName || placeName.trim() === '') {
const error = new Error('Invalid place parameter.');
return next(error);
}
const GAODE_API_KEY = 'b6ba147ffd1e49158d12f7cb16d0f381';
const GAODE_GEOCODE_URL = `https://restapi.amap.com/v3/geocode/geo?address=${encodeURIComponent(placeName)}&key=${GAODE_API_KEY}`;
let location = null; // 定义location变量在try块之前
try {
const response = await axios.get(GAODE_GEOCODE_URL);
if (response.data && response.data.geocodes && response.data.geocodes.length > 0) {
location = response.data.geocodes[0].location;
} else {
throw new Error('Unable to geocode the provided place.');
}
} catch (error) {
return next(error); // 使用next()将错误传递给下一个中间件
}
if (location) {
const [longitude, latitude] = location.split(',');
res.render('map', { latitude: latitude, longitude: longitude });
} else {
res.send(`Unable to find location for ${placeName}`);
}
});
//在app.js的底部,添加以下代码来创建一个错误处理中间件:
app.use((err, req, res, next) => {
console.error(err.stack); // 打印错误堆栈到控制台
res.status(500).send('Something went wrong! ' + err.message);
});
//在Express应用中提供两个新的路由来返回年份和省份的数据,然后在前端使用AJAX请求这些数据
const fs = require('fs');
const path = require('path');
app.use('/year/:year/:province.html', (req, res) => {
const year = decodeURIComponent(req.params.year);
const province = decodeURIComponent(req.params.province);
res.sendFile(path.join(__dirname, 'year', year, `${province}.html`));
});
app.get('/get-years', (req, res) => {
const years = fs.readdirSync(path.join(__dirname, 'year'));
res.json(years);
});
app.get('/get-provinces/:year', (req, res) => {
const provinces = fs.readdirSync(path.join(__dirname, 'year', req.params.year))
.map(file => file.replace('.html', ''));
res.json(provinces);
});
// 新增路由来处理地理编码请求
app.get('/getGeoAddress', async (req, res, next) => {
const placeName = req.query.address; // 从查询参数中获取地址
// 检查place参数是否有效
if (!placeName || placeName.trim() === '') {
const error = new Error('Invalid place parameter.');
return next(error);
}
const GAODE_API_KEY = 'b6ba147ffd1e49158d12f7cb16d0f381';
const GAODE_GEOCODE_URL = `https://restapi.amap.com/v3/geocode/geo?address=${encodeURIComponent(placeName)}&key=${GAODE_API_KEY}`;
let location = null;
try {
const response = await axios.get(GAODE_GEOCODE_URL);
if (response.data && response.data.geocodes && response.data.geocodes.length > 0) {
location = response.data.geocodes[0].location;
} else {
throw new Error('not find this place');
}
} catch (error) {
return next(error);
}
if (location) {
const [longitude, latitude] = location.split(',');
res.json({ latitude: latitude, longitude: longitude });
} else {
res.json({ error: `not find this place ${placeName}` });
}
});
// 从数据库中导出矢量文件到路径
app.get('/getGsonDB', async (req, res, next) => {
const dataCode = req.query.code;
try {
// 首先查询数据库
const result = await db.any('SELECT ST_AsGeoJSON(geom) as geojson_geom FROM xian_vector."CHN_xian" WHERE code = $1', [dataCode]);
if (result && result.length > 0) {
const geojson = JSON.parse(result[0].geojson_geom);
const gsonFilePath = path.join(__dirname, 'public', 'shp', `${dataCode}.gson`);
fs.writeFileSync(gsonFilePath, JSON.stringify(geojson)); // 保存geojson到文件
} else {
console.log(`No vector found for code: ${dataCode} in the database.`);
}
} catch (error) {
console.error("Error while fetching data from database:", error.message);
}
});
//获取矢量文件路径
app.get('/getGsonFile', (req, res, next) => {
const dataCode = req.query.code;
const gsonFilePath = path.join(__dirname, 'public', 'shp', `${dataCode}.gson`);
if (fs.existsSync(gsonFilePath)) {
const gsonData = fs.readFileSync(gsonFilePath, 'utf8');
res.json(JSON.parse(gsonData));
} else {
res.status(404).send('not find json');
}
});
//获取矢量文件路径
app.get('/getGsonFile', (req, res, next)=> {
const dataCode = req.query.code;
// 下面是原有的代码
const gsonFilePath = path.join(__dirname, 'public', 'shp', `${dataCode}.gson`);
if (fs.existsSync(gsonFilePath)) {
const gsonData = fs.readFileSync(gsonFilePath, 'utf8');
res.json(JSON.parse(gsonData));
} else {
res.status(404).send('not find json');
}
});
// 在app.js中添加新的路由来提供矢量文件的下载
app.get('/downloadVector/:code', (req, res, next) => {
const dataCode = req.params.code;
const vectorFilePath = path.join(__dirname, 'public', 'shp', `${dataCode}.gson`);
if (fs.existsSync(vectorFilePath)) {
res.download(vectorFilePath); // 使用Express的download方法
} else {
const error = new Error('not find json');
return next(error);
}
});
//添加一个新的路由来检查矢量文件是否存在
app.get('/checkVectorExistence', (req, res) => {
const dataCode = req.query.code;
const gsonFilePath = path.join(__dirname, 'public', 'shp', `${dataCode}.gson`);
if (fs.existsSync(gsonFilePath)) {
res.json({status: 200, message: "Exists"});
} else {
res.json({status: 404, message: "Not Found"});
}
});