i'm new node.js.
i'm creating simple node/express application serves single web page containing 1 button when clicked makes jquery ajax request express route.
the route callback makes http.get request openexchangerates.org json data containing foreign exchange rates. json output developer tools console window.
the application works on first button click, on subsequent clicks console window displays:
get http://127.0.0.1:3000/getfx net::err_connection_refused
a screen grab of developer tools console window shows result of first click, , second click when connection refused.
the error detail follows:
get http://127.0.0.1:3000/getfx net::err_connection_refused jquery-2.1.3.min.js:4 n.ajaxtransport.k.cors.a.crossdomain.send jquery-2.1.3.min.js:4 n.extend.ajax (index):18 (anonymous function) jquery-2.1.3.min.js:3 n.event.dispatch jquery-2.1.3.min.js:3 n.event.add.r.handle
my simple node/express application follows:
var express = require('express'); var app = express(); var http = require("http"); var data = ""; var json; console.log( "__dirname", __dirname ); app.use( express.static( __dirname + '/') ); var options = { host:"openexchangerates.org", path:"/api/latest.json?app_id=<get free id openexchangerates.org>" }; app.get("/", function( req, res ) { res.sendfile('index.html', { root: __dirname }); }) app.get("/getfx", function(req, res) { console.log("route: getfx"); getfx(res); }) function getfx(res) { console.log("http getfx"); http.get(options, function (response) { response.on("data", function (chunk) { //console.log("data:\n"+chunk); data += chunk; }); response.on("end", function () { json = json.parse(data); console.log("http response end:"); res.end( data ); }); response.on("error", function (e) { console.log("error:\n" + e.message); }); }) } app.listen(3000);
my html index page is:
<!doctype html> <html> <head lang="en"> <meta charset="utf-8"> <title>get fx</title> <script src="http://code.jquery.com/jquery-2.1.3.min.js"></script> <script> $(document).ready(function() { console.log( "document ready"); $("#btnfx").click(function() { console.log('clicked', ); $.ajax({ url : "http://127.0.0.1:3000/getfx", datatype : "json", success : function(json) { console.log("json returned:\n", json); } }); } ); }) </script> </head> <body> <button id="btnfx" style="width:200px">get foreign exchange rates</button> </body>
for openexchangerates.org serve data, free app id required. able resolve may have go through short sign up:
that link here:
https://openexchangerates.org/signup/free
however it's possible mistake glowingly obvious better node/express/jquery knowledge.
many in advance
the way defined data
, json
vars causing subsequent requests fail. since defined them front, requests re-use them, meaning time json.parse data
second request, data
contain 2 valid json strings, making 1 invalid json string. fix this, define data
, json
farther down in callback.
var express = require('express'); var app = express(); var http = require("http"); //var data = ""; //var json; console.log( "__dirname", __dirname ); app.use( express.static( __dirname + '/') ); var options = { host:"openexchangerates.org", path:"/api/latest.json?app_id=<get free id openexchangerates.org>" }; app.get("/", function( req, res ) { res.sendfile('index.html', { root: __dirname }); }) app.get("/getfx", function(req, res) { console.log("route: getfx"); getfx(res); }) function getfx(res) { console.log("http getfx"); http.get(options, function (response) { var data = ""; var json; response.on("data", function (chunk) { //console.log("data:\n"+chunk); data += chunk; }); response.on("end", function () { console.log("http response end:"); json = json.parse(data); res.json(json); }); response.on("error", function (e) { console.log("error:\n" + e.message); }); }) } app.listen(3000);