2015-07-31 3 views
0

У меня есть объект вроде этого:Express.js маршрут и регулярное выражение

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

Я хочу, чтобы создать маршруты для retrive вложенного объекта, как:

хоста/geo.schema или хоста/гео .json

получить весь объект

хост/гео/properties.schema или хост/гео/properties.json

получить

properties: { 
      latitude: {type: 'number'}, 
      longitude: {type: 'number'} 
     } 

ответ

0

Я нашел решение с регулярным выражением ,

app.get('\/[a-zA-Z]+(\/[a-zA-Z]*)+(\.json|\.schema)', function (req, res) { 
     var url = req.url.toLowerCase(); 

     var elements = url.split('/'); 
}); 
0

Вы можете попробовать следующее:

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); 
}); 
+0

Это не работает, если у меня есть: /host/geo/latitude.schema –

Смежные вопросы