How To Read a file in Node JS


How To Read a file in Node JS

In this tutorial, you will learn how to read a file in Node JS. In order to do this, you have to include a built-in module of Node JS known as fs( File System) with the help of require() method as shown below

var fs = require("fs");
fs.readFile("name of the file","encoding","function(err,data))

Please note that the readFile() function contains three parameters

  • the filename that has to be read using Node JS.
  • Encoding defines the type of encoding format of the file that has to be read.
  • The third parameter is a callback function that contains two arguments: err and data. If there is any error in your file then err will hold the error message. On the other hand, the data parameter will hold the contents of the file that has to be read if there is no error.

Also read, the Node JS HTTP server example

Steps to read a file in Node JS with an example

  • Create an HTML file or a text file where the node JS file resides. (Mentioned Below)
  • Put some contents in a file that has to be read.
  • Now, create a Node JS file. (Mentioned Below)
  • Open the command terminal and go to the location where your node JS file is stored and instantiate the node JS file as shown below

node filename.js

Now, press Enter.

You will see the output in your browser as shown below

Please read me with Node JS

This is a paragraph.

readfile.js

var http = require('http');
var fs = require('fs');
http.createServer(function(req,res){
//read the HTMl file
	fs.readFile('test1.html','utf-8',function(err,data){

		res.writeHead(200, {'Content-Type': 'text/html'});
	    res.write(data);
	    res.end();
	});
}).listen(8080);

test1.html:-

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>

<h1>Please read me with Node JS</h1>
<p>This is a paragraph.</p>

</body>
</html>

Explanation of the above file

  • First of all, I have included http and fs modules respectively. http is used to access data via hypertext transfer protocol which means over the web browser. On the other hand, the fs module is used to read the contents of the file.
  • Now, the createServer() object is created because we have to get the response from the server.
  • Now, pass the HTML file or any text file inside the readFile() function as the first parameter.
  • Now, type the encoding format such as utf-8. This is optional.
  • Now pass data inside the res.write(data) to display the result in the browser.

Also read, How to Import Custom Module in Node JS

Conclusion:- I hope this will help you to understand the concept. If there is any doubt then please leave a comment below.


Leave a Comment

KUPU178

RATU311

RATU311

RATU311

RATU311

RATU311

sabung ayam online

THOR311

parlay

RATU311

KUPU178

RATU311

RATU311

RATU311

RATU311

KUPU178

KUPU178

KUPU178

RATU311

RATU311

CERI188

CERI188

KUPU178

RATU311: Alternatif karnpuracollege

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

RATU311

RATU311

RATU311

judi bola

live casino

sv388

sv388

sabung ayam online

sabung ayam online

Thor311 Terbaru

CERI188: Taruhan Bola Sbobet

THOR311 DEMO SLOT

THOR311 MAHJONG

RATU311: BONUS SABUNG AYAM

CERI188

parlay

KUPU178

RATU311

THOR311

RATU311

RATU311

RATU311

RATU311

RATU311

CERI188

CERI188: Masuk Akun Slot Super Cepat

KUPU178: Slot Online Mahjong Bet 200

THOR311 PRO

THOR311

CERI188

CERI188

CERI188

CERI188

CERI188

CERI188

CERI188

CERI188

THOR311

HOKI311

HOKI311

RATU311

RATU311

KUPU178

THOR311

KUPU178

THOR311

RATU311

HOKI311

KUPU178

KUPU178

RATU311

THOR311 TOTO4D

KUPU178

Live Casino Roullate

RATU311

RATU311

RATU311

RATU311

THOR311

THOR311

THOR311

RATU311

CERI188

DAFTAR KUPU178

LINK DAFTAR CERI188

CERI188

APK KUPU178

KUPU178

CERI188

CERI188

RATU311

KUPU178

THOR311 DOMINO QQ

judi bola online

Mix Parlay

CERI188

CERI188

CERI188

RATU311

live casino online

RATU311

Mix Parlay

Sabung Ayam Online

Slot PG Soft

CERI188

RATU311

RATU311

CERI188

THOR311

CERI188

THOR311

Game Thor311

THOR311

HOKI311

live casino

sv388

HOKI311

HOKI311

slot mahjong

Thor311 Alternatif

RATU311

HOKI311

HOKI311

HOKI311

HOKI311

CERI188

THOR311 RESMI

THOR311 AKSES

THOR311

THOR311 RTP

parlay

HOKI311

THOR311

Thor311 Aplikasi

RATU311 NONTON BOLA

CERI188 Link RTP LIVE

THOR311

Thor311 Live Casino

CERI188

akses VIP THOR311

THOR311

link daftar THOR311

THOR311

situs thor311

RATU311

HOKI311 Link SABUNG AYAM RESMI

HOKI311 Link QRIS EWALLET

RATU311

LINK CERI188

parlay

HOKI311 Link RTP Gacor

THOR311

HOKI311 Link Apk Resmi

CERI188

KUPU178 Link Daftar

CERI188

CERI188

CERI188

CERI188

CERI188

HOKI311

RATU311

RATU311

HOKI311 Lapak Arena Slot

RATU311 judi bola

HOKI311 Link Spaceman Online

HOKI311 Link Daftar Resmi

CERI188 Toto SGP Online

KUPU178

HOKI311 TEMBAK IKAN ONLINE

KUPU178

HOKI311 Link Login Alternatif

HOKI311 Live Casino Online

HOKI311 Situs Slot Gacor

THOR311

THOR311

situs judi bola

Ceri188 Game Online

CERI188

link login CERI188

link login CERI188

CERI188

THOR311

HOKI311

HOKI311

CERI188

parlay

THOR311

RATU311

HOKI311

HOKI311

HOKI311

RATU311

THOR311

KUPU178

HOKI311

KUPU178

KUPU178

HOKI311

parlay

slot gacor

kupu178 link alternatif

kupu178 slot

kupu178 daftar

kupu178 login

link daftar kupu178

judi bola

HOKI311

RATU311

link alternatif RATU311

Link daftar RATU311

link login RATU311

RATU311

RATU311

KUPU178

KUPU178

THOR311

KUPU178

KUPU178

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

THOR311

KUPU178

KUPU178

HOKI311

THOR311

KUPU178

THOR311

THOR311

slot thailand

judi bola

THOR311

THOR311

KUPU178

THOR311

THOR311

slot gacor

slot gacor

yakuza303

unobet77

slot gacor

slot gacor

RATU311

parlay

parlay

slot gacor

parlay

THOR311