Guy Ben-Shahar
Guy Ben-Shahar

Reputation: 231

How to read parameters from URL in a specific way with express JS

I created a server with the express package, and I'm trying to read in a specific way the parameters from the URL. The URL goes like this: http://127.0.0.1:8080/screen=3 (no '?' as the sign for parameters). I need to save the number of the screen in a variable.


I tried this:

var express = require('express');
var app = express();

app.get('/screen:sceenNum', function (req, res) {
    var temp = req.sceenNum;
    res.send(temp); //for checking on the browser
});

I also tried this, but he must get '?' in the URL:

app.get('/', function(req, res) {
    var screenNum = req.param('screen');
    res.send(screenNum);
});

Can anyone please have a solution? Thank you

Upvotes: 2

Views: 56

Answers (1)

mscdex
mscdex

Reputation: 106736

You can access route/url parameters with the req.params object.

So a route like /screen:screenNum would accept urls like /screen3 and you would access 3 via req.params.screenNum.

Similarly, if you want to use the equals, just add that: /screen=:screenNum and the number is accessed the same.

Upvotes: 1

Related Questions