nicholaswmin
nicholaswmin

Reputation: 22949

Use route path as parameter in Express

I'm on a Node/Express/Socket.io stack.

I have a rooms-like collaborative setup going on.

Right now when my users type in the url mysite.io/editor/?roomId=myRoom, I grab the parameter roomId via var room = url.parse(socket.handshake.headers.referer, true).query.roomId.


I don't like using parameters in the URL (I'd like to keep it as clean as possible). I'd like to know if it's possible to do the same via the route path instead of a URL query string parameter.

E.g

mysite.io/editor/myRoom

Upvotes: 0

Views: 430

Answers (2)

Mi Ke Bu
Mi Ke Bu

Reputation: 224

// url mysite.io/editor/?roomId=myRoom
app.get('/editor/', function(req, res) {
   console.log(req.query.roomId);
   //write "myRoom"
});

Upvotes: 0

Hiren S.
Hiren S.

Reputation: 2832

if you are having routes for express, you can do this :

app.get('/editor/:room', function(req, res) {
 //use  req.params.room;
});

Upvotes: 3

Related Questions