Node: Get var from JS file from URL

Stanley Cup Phil

I have a request call that will call a javascript file that contains a var:

var url = 'http://' + _environment + '/services.js';

request(url, function (error, response, body) {
   if (!error && response.statusCode === 200) {
       _services = body;
   }

   console.log(_services);
});

This will log something like the following:

var services = {
     "production": {
          "global": {
               "id": "id",
               "test2": "http://test.com",
               "test": "/test/test.test",
               "test1": "test"
          }
     }
}

So to be exact, this line is equivalent to what I am ending up with:

var _services = var services = {
     "production": {
          "global": {
               "id": "id",
               "test2": "http://test.com",
               "test": "/test/test.test",
               "test1": "test"
          }
     }
}

How to I pass a var from a js file located at some url to my code?

I wrestled a bear once.

you could use exec(_services), but a better idea would be to just drop the var part and return regular JSON, then parse the JSON.

Make it return this..

{
     "production": {
          "global": {
               "id": "id",
               "test2": "http://test.com",
               "test": "/test/test.test",
               "test1": "test"
          }
     }
}

Then just do var services = JSON.parse(_services);


Alternatively, you can create a script tag and append it..

// This is with jQuery, you can convert to standard JS
$("body").append("<script>"+_services+"</script>");

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Node js Get folder path from a file

From Dev

How to send a file from remote URL as a GET response in Node.js Express app?

From Dev

Importing a json file from a url using node js (express)

From Dev

Node JS does not correctly download file from github url

From Dev

Get parameters from Stripe page redirect URL Node.js

From Dev

Node.js / REST: How to get _id from url

From Dev

How to get id from URL as variable? In Node.js

From Dev

Node.js - Get variable from another loaded file

From Dev

How to get var value from a text file?

From Dev

How can I get Javascript Source(js file) from url?

From Dev

retrieving var from URL

From Dev

Node.js, parse filename from URL

From Dev

node.js + mysql Query from url

From Dev

node.js download image from url

From Dev

Downloading a file from a Node.JS API REST (express) with React.JS (Axios Get)

From Dev

Download file from url and upload it to AWS S3 without saving - node.js

From Dev

Node.js: Download an (auto-generated) file from a PHP URL

From Dev

Accessing javascript file from Node JS - Beginner

From Dev

call two file from another with node js

From Dev

Reading from a file in node.js

From Dev

executing node command from js file

From Dev

bash how to get var from file with multiple lines

From Dev

get value from all array var in a php file

From Dev

bash how to get var from file with multiple lines

From Dev

How to get a string from external PHP file and set it as var in JavaScript

From Dev

using javascript, angular.js, and sql.js, how do I get a file object from a url?

From Dev

How to get data from the client to the server (without url parameters) in Node.js?

From Dev

Node.js: get path from the request

From Dev

Node JS get values from multiple promise

Related Related

  1. 1

    Node js Get folder path from a file

  2. 2

    How to send a file from remote URL as a GET response in Node.js Express app?

  3. 3

    Importing a json file from a url using node js (express)

  4. 4

    Node JS does not correctly download file from github url

  5. 5

    Get parameters from Stripe page redirect URL Node.js

  6. 6

    Node.js / REST: How to get _id from url

  7. 7

    How to get id from URL as variable? In Node.js

  8. 8

    Node.js - Get variable from another loaded file

  9. 9

    How to get var value from a text file?

  10. 10

    How can I get Javascript Source(js file) from url?

  11. 11

    retrieving var from URL

  12. 12

    Node.js, parse filename from URL

  13. 13

    node.js + mysql Query from url

  14. 14

    node.js download image from url

  15. 15

    Downloading a file from a Node.JS API REST (express) with React.JS (Axios Get)

  16. 16

    Download file from url and upload it to AWS S3 without saving - node.js

  17. 17

    Node.js: Download an (auto-generated) file from a PHP URL

  18. 18

    Accessing javascript file from Node JS - Beginner

  19. 19

    call two file from another with node js

  20. 20

    Reading from a file in node.js

  21. 21

    executing node command from js file

  22. 22

    bash how to get var from file with multiple lines

  23. 23

    get value from all array var in a php file

  24. 24

    bash how to get var from file with multiple lines

  25. 25

    How to get a string from external PHP file and set it as var in JavaScript

  26. 26

    using javascript, angular.js, and sql.js, how do I get a file object from a url?

  27. 27

    How to get data from the client to the server (without url parameters) in Node.js?

  28. 28

    Node.js: get path from the request

  29. 29

    Node JS get values from multiple promise

HotTag

Archive