User Tools

Site Tools



Python Tutorials

WebGL Tutorials

Node.js Tutorials

Node.js in WebStorm

Martin Christen

View Martin Christen's profile on LinkedIn


Timeline of @MartinChristen

MartinChristen avatar

RT @snebiker: #PlayGameLieschtel - brilliant event by Kantonsbibliothek Baselland #KBL to get kids into libraries, including family #gamede…
About 2 days, 13 hours ago by: Martin Christen (@MartinChristen)

MartinChristen avatar

Only a handful tickets left for @PyConDE in Karlsruhe (October 24-28, 2018) - The schedule is now available at…
About 5 days, 6 hours ago by: Martin Christen (@MartinChristen)

MartinChristen avatar

IAMCP & @swissmade_sw Augmented Reality event just started #ar #AugmentedReality #Basel…
About 6 days, 12 hours ago by: Martin Christen (@MartinChristen)

Node.js Tutorials - Part 0

Node.js Tutorial 5

Reading and writing files is something you can't do in standard JavaScript. In node.js file handling comes with the built in file system module called “fs”. The module is loaded using:

var fs = require('fs');

Writing Files

Writing a whole file can be done with the “writeFile” function. This requires a buffer containing the whole contents of the file. There are two versions of this function: The first version “writeFile” is asynchronous and the second version “writeFileSync” is synchronous.

var data = "Hello World, this is a text...";
var filename = "file.txt";

fs.writeFile(filename, data , function (err) {
  if (err) throw err;
  console.log('Ok. File written...');

Reading Files

Reading a whole file can be done with the “readFile” function. There is also a synchronous and an asynchronous version of this function.

var filename = "file.txt";

fs.readFile(filename, function (err, data) {
  if (err) throw err;
  console.log("" + data);

node/tutorial05.txt · Last modified: 2013/09/16 22:05 by mchristen