2

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

1
  • the question mark is how query string parameters work? why not use it? Commented Dec 7, 2015 at 18:38

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.