Home Nodejs Node Js – Reading File Simple Example

Node Js – Reading File Simple Example

by therichpost
2 comments
nodejs

Hello to all, welcome to therichpost.com. In this post, we will do, Node Js – Reading File Simple Example.

Nodejs is an open-source server environment which runs Javascript on the server.

I was just practicing node js and I liked it very much and seems interesting so that is why, I am sharing code on my blog.

I am sharing the code for to read file with nodejs and for this, we will file-system module in our nodejs application.

Very first, we need to install nodejs into our system and here is the link:

https://nodejs.org

 

Here is the working and tested code and you need to add this into your js file:

 

1. You need add the below code into your YourFileName.js file:

var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
  fs.readFile('applications.html', function(err, data) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write(data);
    res.end();
  });
}).listen(8080);

 

2. After it, you need to run below command into your terminal to execute js file:

 node YourFileName.js

 

3. After it, run below url into your browser:

http://localhost:8080/

If you have any query related to this post, then please comment below or ask question.

Thank you,

Happy Coding,

Therichpost

You may also like

2 comments

jasmeen October 15, 2018 - 4:49 am

Simple and easy

Reply
Ajay Malhotra October 15, 2018 - 3:50 pm

Thank you jasmeen..

Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.