12

This is my express app running on Node.js

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

app.get('/', function(req, res){
  res.send('id: ' + req.query.id);
});

app.listen(3000);
console.log('working...');

//so i need url id as var
var gotID = req.query.id

//want to use this data to another js file
module.exports = gotID;

So I want that URL id as my variable.

Which is look like this URL

http://localhost:3000/[id that i want]

So what should I do?

I'm new to Node.js

1 Answer 1

36

This should work for you:

app.get('/:id', function(req, res) {
    res.send('id: ' + req.params.id);
});

req.query is used for search query parameters (i.e. everything after ? in http://something.com/path?foo=var)

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

2 Comments

Thanks for help but i had problem about url. this sytanx something.com/path?foo=var didnt work and i used like that something.com/path/var
If you need query parameters you should use req.query. req.params is for path parameters

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.