1

I am connecting to an api and pulling a set of json data. the javascript outputs the json as the variable feedData and when i include that in a html i get the json on a html page as expected. What i want to do is output it as a json endpoint. I tried to get fancy and when i tried:

var express = require('express');
var app = express();
app.get('/', function (req, res) {
  res.send(feedData);
});
app.listen(3000, function () {
 console.log('posting data');
});

the problem is that i need to import an api.js file and when i attempt to load this i get an error related to the api.js file which is windows is not defined. Like i said, the html

 document.getElementById('mydiv').innerHTML += JSON.stringify(feedData, undefined,2);

then

<pre id="mydiv"></pre>

works fine but i will also to recall every x seconds because this is a live json feed.

Currently, i just decided to connect via python, loading it into mongodb and creating a nodejs endpoint from there, which works fine, but it seems there should be a way here.

2 Answers 2

1

Try using res.json, res.json(feedData); to send objects. If you want to send a string just, use res.send

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

1 Comment

i still get the error when using node, Reference error: window is not refined which points back to the api.js file and the line for "window[JSON_REQUESTS_PROPERTY_STR] = {}" it works fine with jquery and html, but node/ express seems to cause problems
1

Instead of

res.send(feedData);

use

res.json(feedData);

Hope this helps

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.