Categories

Monday, November 18, 2024
#919814419350 therichposts@gmail.com
Nodejs

Node Js – Reading File Simple Example

Node Js - Reading File Simple Example

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

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 19, MedusaJs, Next.js, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

2 Comments

Leave a Reply

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