2

running this code giving me this error, trying to fig out since an hour but failed

var http = require('http'); var url = require('url'); var fs = require('fs'); var port = 3010; http.createServer(function(req, res){ var query = url.parse(req.url,true).query; console.log(query); var file = query.f + query.t; //var file = "eurusd_m1.json"; console.log(file); var eurusd; fs.readFile('data/' + file + '_m1.json', function(err,data){ if (err){ console.log(err); } eurusd = JSON.parse(data); console.log(eurusd); }); res.writeHead(200,{'content-type':'text/plain'}); res.end("helllo owrld"); }).listen(port); console.log("server running at port 3010.."); 

it's giving me below result:


server running at port 3010.. { f: 'eur', t: 'usd' } eurusd {} NaN { [Error: ENOENT, open 'C:\Users\Administrator\Documents\zeromq\data\NaN_m1.json'] errno: 34, code: 'ENOENT', path: 'C:\\Users\\Administrator\\Documents\\zeromq\\data\\NaN_m1.json' } undefined:1 undefined ^ SyntaxError: Unexpected token u at Object.parse (native) at C:\Users\Administrator\Documents\zeromq\dataserver.js:17:17 at fs.js:207:20 at Object.oncomplete (fs.js:107:15) 

1
  • NaN_m1.json. That's not your filename is it? The var file = query.f + query.t is evaluating to NaN which is probably not what you want. Print query to see if it holds the values you expect. Commented Sep 15, 2013 at 22:20

1 Answer 1

2

What you're seeing is the client's request to favicon.ico. There are two requests being performed here (hence, you see the log twice). The browser asks for the page and for the favicon.ico file representing the tiny little icon you see when you bookmark and to the left of the address bar :)

Your problem is in var file = query.f + query.t; - this makes sense when you access the page, but does not make sense when accessing favicon.ico implicitly by your browser.

You are just console.loging the error instead of returning from it, which means you JSON.parse on an undefined value - which throws a syntax error as defined in the spec (just try opening the console and typing JSON.parse(window.x) in your browser)

(You can see this by the ENOENT when it tries to open "NaN_m1.json")

You can add this to your server before calling url.parse, but if you intend to make it bigger - proper routing is probably better:

if (req.url === '/favicon.ico') { r.writeHead(200, {'Content-Type': 'image/x-icon'} ); return r.end(); } 
Sign up to request clarification or add additional context in comments.

6 Comments

thanks Benjamin for a quick response, but how come its NaN, i can see it before giving it to readFile in console.log that it's spitting eurusd, then why it's taking it as NaN. how should i correct it any idea.
@zishan try console.log(req.url) on top - see what URLs are being accessed. There are two requests being sent by the browser and not one (hence the double logging for the rest). Detect if the url matches the route you want and act accordingly :)
@zishan the NaN you're seeing is undefined+undefined by the way - which is the query parameters for favicon.ico . If I may suggest an introductory book, I've found nodebeginner.org quite useful.
thanks Benjamin for th enodebeginer,I already read few pages and order soon, it a nice to have book. here is my req.url console.log -> /?f=eur&t=usd and here is the url i am passing localhost:3010/?f=eur&t=usd
@zishan if this has resolved your issue, please consider accepting it, if it has not - please consider elaborating on why. Thanks and happy coding :)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.