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

@PostFinance If you are interested in Python - the @EuroPython conference, largest Python conf in Europe with over…
About 2 days, 23 hours ago by: Martin Christen (@MartinChristen)

MartinChristen avatar

RT @opengeospatial: OGC seeks public comment on update to GeoTIFF standard. This minor revision to GeoTIFF enables the use of newer coordin…
About 5 days, 12 hours ago by: Martin Christen (@MartinChristen)

MartinChristen avatar

RT @europython: Early-bird tickets sold out in less than 10 minutes. Amazing ! Sales were so fast, we ended up selling 275 tickets instead…
About 6 days, 15 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