2012-12-13 17 views
8

Mi piacerebbe "sbarazzarsi" del router in node.js. Attualmente, ciò che ho è qualcosa che assomiglia a questo:Wildcard nel router Express/node.js

app.get '/thing1', (req, res) -> 
    res.render 'thing1' 

app.get '/thing2', (req, res) -> 
    res.render 'thing2' 

C'è un modo per comprimere questi per qualcosa di simile:

app.get '/(*)', (req, res) -> 
    res.render '(*)' 

PS: sto usando CoffeeScript, ma una risposta in qualsiasi lingua è OK

risposta

27
app.get('/:thing', function (req, res) { 
    res.render(req.params.thing) 
}) 
+6

Si noti che l'uso di ": cosa" non corrisponde a un percorso come "/ percorso/con/barre". –

+0

+1 bella funzionalità. – dwerner

Problemi correlati