我知道如何使用server.js中的app.post()从HTML表单读取值。
但是现在位置(经度和纬度)是在客户端生成的,而在服务器端则需要。
所以我的问题是,如何将位置值从索引传递到server.js?
我猜我的app.get()必须修改:
app.get('/', function(req, res) {
// get values from index
// ...
console.log("Yeehaa!" + latitude + " " + longitude)
// Render index.ejs
res.render('index',{locs:nearbyLocations})
});
Thx 图片
答案 0 :(得分:1)
将经度,纬度作为查询参数:
服务器端代码:
ON
客户端作为链接:
app.get(
'/locations/nearby',
async (req, res) => {
const {latitude, longitude} = req.query;
console.log("Yeehaa!" + latitude + " " + longitude)
const locations = ... getting locations nearby ...;
res.render('index', {locations});
});