2012-12-13 12 views
8

Chciałbym "pozbyć się" routera w node.js. Obecnie, co mam to coś, co wygląda tak:Wildcard w Express/node.js router

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

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

Czy istnieje sposób aby zwinąć je do czegoś takiego:

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

PS: Używam coffeescript, ale odpowiedzią na każdy język jest OK

Odpowiedz

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

Należy pamiętać, że użycie ": thing" nie będzie pasować do ścieżki takiej jak "/ path/with/slashes". –

+0

+ 1 funkcja ładna. – dwerner