繁体   English   中英

Express.js路由和正则表达式

[英]Express.js route and regex

我有一个这样的对象:

geo: {
        description: 'Geolocalisation',
        properties: {
            latitude: {type: 'number'},
            longitude: {type: 'number'}
        }
    }

我想创建路由来检索嵌套对象,例如:

host / geo.schema或host / geo.json

得到整个对象

host / geo / properties.schema或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('/');
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM