0

I have the following URL:

http://localhost:3000/?url=test

In my routes/index.js I'm great the url parameter and trying to console.log:

var express = require('express');
var router = express.Router();

var url_param;

router.get('/:url', function (req, res) {
    var url_param = req.params.url;

});
var url;
var url = url_param
console.log(url);

However it doesn't log anything. In my terminal I get it performing the GET function correctly:

GET /?url=test 304 4.169 ms - -

Am I missing something?

Thanks!

Chris Olson
  • 1,021
  • 4
  • 12
  • 19
  • https://stackoverflow.com/questions/28147110/javascript-callback – Will Jun 22 '17 at 00:13
  • define `url_param` before the `router.get` line and it will be available outside that block to log. – mjw Jun 22 '17 at 00:14
  • @mjw Sorry, should have mentioned that, I am doing that. – Chris Olson Jun 22 '17 at 00:16
  • You are definitely not doing that in the code you've posted. Please post the code with the issues so we can correctly diagnose the issue. – mjw Jun 22 '17 at 00:20
  • Yes, as @mjw has said, please post the actual code... but also, some extra wouldn't hurt - for example, what is "router"? I don't want to make assumptions that turn out to be incorrect... – NightCabbage Jun 22 '17 at 00:21
  • Ok, updated with more code. Hope this helps! – Chris Olson Jun 22 '17 at 00:24
  • You need to move the `console.log()` line into the function, i.e. up by three lines. Your `.log()` right now is called when you start the server, then never again. –  Jun 22 '17 at 00:25

3 Answers3

0

(I am guessing that this will work.) Try to write you console.log inside your function. Like

router.get('/:url', function (req, res) {
var url_param = req.params.url;
console.log(/* your code */ );
});
  • The problem is, I need to use that var (url_param) outside of the function. – Chris Olson Jun 22 '17 at 00:27
  • @ChrisOlson You are passing a `function` to `router.get()`, and this function is executed when the resource is requested, at some arbitrary point in the future. Any code that needs to run then has to go inside that function. –  Jun 22 '17 at 00:29
  • You redeclare "url_param" as a new variable inside your function. Remove var in front of "url_param" inside the function to address the "url_param" in the outer scope. – scope-username Jun 22 '17 at 00:32
0

Here's how to store the value and use it somewhere else:

var express = require('express');
var router = express.Router();

var url;

// http://localhost:3000/url
router.get('/url', function(req, res) {
  res.send("the stored url is " + url);
});

// http://localhost:3000/?url=x
router.get('/:url', function(req, res) {
  url = req.params.url;
  console.log(url);
  res.send("url stored");
});
0
  1. move the console.log() inside the route.get function.
  2. Even though you have to move the console.log(); already inside your router function you declared a different variable var url_param by so doing they don't have same reference.

Why wouldn't it work outside the route.get function?

The moment you run 'node thisfile.js' everything on the script will be processed, however router.get function will be waiting to receive an event which will only be triggered once the url is visited.

Thus without the router.get function receiving an event url_param remains undefined. So to get the url param you need to visit the url it matches.

    var express = require('express');
    var router = express.Router();
    var url_param;
    router.get('/:url', function (req, res) {
        url_param = req.params.url;
        console.log(url_param);//TO BE HONEST This will work
    });
console.log(url_param);//TO BE HONEST THIS WOULDNT WORK
Paul Okeke
  • 1,384
  • 1
  • 13
  • 19