Express.js 路由和正则表达式
Express.js route and regex
我有一个这样的对象:
geo: {
description: 'Geolocalisation',
properties: {
latitude: {type: 'number'},
longitude: {type: 'number'}
}
}
我想创建路由来检索嵌套对象,例如:
host/geo.schema or host/geo.json
获取整个对象
host/geo/properties.schema or host/geo/properties.json
获得
properties: {
latitude: {type: 'number'},
longitude: {type: 'number'}
}
您可以尝试以下方法:
var geo = {
description: 'Geolocalisation',
properties: {
latitude: {type: 'number'},
longitude: {type: 'number'}
}
}
app.get('/host/geo.(schema|json)', function (req, res, next) {
return res.status(200).json(geo);
});
app.get('/host/geo/properties.(schema|json)', function (req, res, next) {
return res.status(200).json(geo.properties);
});
我找到了使用正则表达式的解决方案。
app.get('\/[a-zA-Z]+(\/[a-zA-Z]*)+(\.json|\.schema)', function (req, res) {
var url = req.url.toLowerCase();
var elements = url.split('/');
});
我有一个这样的对象:
geo: {
description: 'Geolocalisation',
properties: {
latitude: {type: 'number'},
longitude: {type: 'number'}
}
}
我想创建路由来检索嵌套对象,例如:
host/geo.schema or host/geo.json
获取整个对象
host/geo/properties.schema or host/geo/properties.json
获得
properties: {
latitude: {type: 'number'},
longitude: {type: 'number'}
}
您可以尝试以下方法:
var geo = {
description: 'Geolocalisation',
properties: {
latitude: {type: 'number'},
longitude: {type: 'number'}
}
}
app.get('/host/geo.(schema|json)', function (req, res, next) {
return res.status(200).json(geo);
});
app.get('/host/geo/properties.(schema|json)', function (req, res, next) {
return res.status(200).json(geo.properties);
});
我找到了使用正则表达式的解决方案。
app.get('\/[a-zA-Z]+(\/[a-zA-Z]*)+(\.json|\.schema)', function (req, res) {
var url = req.url.toLowerCase();
var elements = url.split('/');
});