7

I am new to javascript and I would like to specify a javascript program to read from a file and print the contents of the file to a console?.This is the code I have written below and am getting an error,please what's wrong with it?

var express = require('express');

var app = express.createServer(express.logger());

app.get('/',function(request,response){

     var fs = require('fs');
     var buffer = new Buffer(fs.readFileSync('index.html','utf8'));

         response.send(Buffer.toString());

});

   var port = process.env.PORT || 5000;
   app.listen(port,function()
{
    fs.readFileSync();
    console.log("Listening on"+ port);
}
);
falsetru
  • 357,413
  • 63
  • 732
  • 636
Omiye Jay Jay
  • 409
  • 5
  • 10
  • 19
  • 1
    Are you talking about Node? – Asad Saeeduddin Aug 01 '13 at 23:25
  • possible duplicate of [is it possible to read a file using javascript?](http://stackoverflow.com/questions/5028931/is-it-possible-to-read-a-file-using-javascript) – raina77ow Aug 01 '13 at 23:25
  • yes I am talking about node – Omiye Jay Jay Aug 01 '13 at 23:27
  • Have you done any research at all? Have you tried anything? –  Aug 01 '13 at 23:28
  • 1
    no I havent done any research.Could you help please – Omiye Jay Jay Aug 01 '13 at 23:29
  • no...I am kind off new in programming and I am using node.js not Ajax. – Omiye Jay Jay Aug 01 '13 at 23:33
  • 1
    "no I havent done any research.Could you help please": you must be new around here. that's not how we do things... – dandavis Aug 01 '13 at 23:36
  • Hi and welcom to StackOverflow. The community here will answer questions that show some research an knowledge of the topic. It's not a tutorial website. It is a resource to use when you have a specific problem, alongs the lines of ... "I tried to do this, but it didn't work. I expected result "a" but got result "b" instead. Where did I go wrong?" Please read http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist for more info. – Jon P Aug 02 '13 at 00:39
  • What was the error? Have you installed [express](http://expressjs.com/) and read [Node.js API document](http://nodejs.org/api/fs.html#fs_fs_readfile_filename_options_callback) about `fs`? Sorry if this is rude, but I suggest you to read tutorials and documentations about JS, Node.js, and express. There are a lot of problems in this code. – JiminP Aug 03 '13 at 15:40
  • Okay,I would go and read it.Thanks – Omiye Jay Jay Aug 03 '13 at 15:44
  • P.S. If you are new to programming and/or JavaScript, I recommend you to learn from [Codecademy](http://www.codecademy.com/tracks/javascript). :) – JiminP Aug 03 '13 at 15:51
  • Have seen the error,I used response.send(Buffer.toString()) instead of response.send(buffer.toString()). – Omiye Jay Jay Aug 03 '13 at 16:02

1 Answers1

12

Use the readFile method of the fs object to read the file, then use console.log to print it:

/* File System Object */
var fs = require('fs');

/* Read File */
fs.readFile('foo.json', bar)

function bar (err, data)
  {
  /* If an error exists, show it, otherwise show the file */
  err ? Function("error","throw error")(err) : console.log(JSON.stringify(data) );
  };

For instance, if it is named loadfiles.js, run it as such:

node loadfiles.js
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
  • 4
    If you just want string do `data.toString('utf8')` – Simon Franzen Mar 28 '18 at 12:21
  • 1
    Keep in mind, there is also a synchronous version: readFileSync(). As the sample is written above, the script will end before the bar() function ever gets called. – Mike Hedman Sep 29 '19 at 19:05
  • @MikeHedman I do not understand what your point is. `readFile` is asynchronous and JS is asynchronous all over so there is a fit. – Timo Apr 18 '22 at 08:46
  • @SimonFranzen your answer is gold with `toString`, with `json.stringify` you get numbers as output which is not what we want. – Timo Apr 18 '22 at 08:47