Trouble exporting a primitive in node

Trouble exporting a primitive in node
0

#1

Seems like Node has no trouble exporting functions, but string variables are not so easy. In the code below, when I attempt to access long_url in index.js it interpolates as undefined in the pug template:

// urls.js
const express = require("express");
const router = express.Router();

module.exports.long_url = '';

router.get('/', (request, response) => {
    const long_url = request.query.long_url;
    console.log(long_url);
    response.redirect('../result');
    response.send(long_url);
});

module.exports = router;
// index.js
var express = require('express');
var router = express.Router();
const long_url = require('./urls').long_url;

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

router.get('/result', function (req, res, next) {
  res.render('result', { title: "Results", short_url : long_url })
});

module.exports = router;

What is the correct way to export and import this?

Thanks


#2

It looks like you’re overwriting module.exports when you set it to router. Try to put module.exports.long_url = '' after you set it to router.


#3

Ok, urls.js now ends with these lines:

module.exports = router;
module.exports.long_url = '';

There is another issue because the output is blank now. I just tested using another file to export a string variable and it did output correctly though. So something, probably my get request code, is wrong.


#4

The problem is with your export. long_url is never set to anything, you’re creating a new variable with const long_url instead of reusing that one you exported.


#5

Ah, you are right. I removed const from the line inside the function definition.

It still outputs blank though, so there is another issue

A console log in urls.js captures long_url. the log statement in index.js does not, it shows blank. so it is not exporting/importing correctly still


#6

I think the premise is wrong in the first place, by doing this you’re kind of using global variables. I don’t think this will work, but it may if you’re using webpack. The problem is that when index.js reads long_url it is empty, it won’t change later when router.get('/'... is called in url.js.

Why are you grabbing long_url from urls.js? Can’t you get it from the req inside router.get('/result')?


#7

You are right. I did a refactor on index.pug to remove the reference to /urls

block content
    div(id="container")
        h4 Enter a url
        form(method='get', action="/result")
            input(type='text' name="long_url" value="testval",  size="55", placeholder="copy/paste url here")
            button Shorten

And now index.js accesses the query params directly:

var express = require('express');
var router = express.Router();
const test_export = require('../text_exports');

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

router.get('/result', function (req, res, next) {
    const long_url = req.query.long_url;
    console.log('index.js: 12', long_url);
    res.render('result', { title: "Results", short_url : long_url })
});

module.exports = router;

output shows undefined now. I’m sure it’s a small issue at this point


#8

Try logging req and req.query.


#9

Yep, req.query logs a blank object. Ok, that narrows things down at least


#10

I believe req.query returns only the url params, is that what you’re trying to get?

Example: site.com/?something=1 should return { something: 1}.

If you want to get the url per si, you can use req.url or req.originalUrl.


#11

Yes and it is working. Something is just not refactored right. Here is some console output:

url-shortener:server Listening on port 3000 +0ms
GET /urls?long_url=testval 302 10.995 ms - 62
index.js: 12 undefined
index.js: 13 IncomingMessage {

lol there it is. It’s still making requests to /urls?


#12

You’re requesting /urls but your router is listening for /result.