Node.js
Install NPM: 3 Advantages of NPM
To install npm, Node.js is required. Node.js has emerged as a popular platform for developing server-side applications and running JavaScript on the server. One of the key reasons behind its widespread adoption is the open-source libraries and tools available through the Node Package Manager (NPM). In this article, we will guide you through the process […]
Install angular
“Easily Install Angular and Unlock the Power of Web Development!” Angular is a powerful JavaScript framework used to create dynamic web applications. It is an open-source framework maintained by Google and a community of individual developers and corporations. Angular is a great choice for developing single-page applications as it provides a structured approach to building […]
Read file in Node.js
Asynchronously reads the entire contents of a file. var fs = require(‘fs’); var path = require(‘path’); exports.testDir = path.dirname(__filename); exports.fixturesDir = path.join(exports.testDir, ‘fixtures’); exports.libDir = path.join(exports.testDir, ‘../lib’); exports.tmpDir = path.join(exports.testDir, ‘tmp’); exports.PORT = +process.env.NODE_COMMON_PORT || 12346; // Read File fs.readFile(exports.tmpDir+’/world.csv’, ‘utf-8’, function(err, content) { if (err) { got_error = true; } else { console.log(‘cat returned […]
Node.js Response.WriteHead function
Up until now all we’ve been sending into the writeHead function is the status code. However, it can take additional parameters like ‘Content-Length’, ‘Content-Type’. var http = require(‘http’); var fs = require(‘fs’); http.createServer(function(request, response) { response.writeHead(200, {‘Content-Type’: ‘text/html’} ); fs.readFile(‘index.html’, function(err, contents) { response.write(contents); response.end(); }); }).listen(8080);
Node.js Read File from Server
Now, showing you how to create an HTTP non-blocking server and how to read a file of the non-blocking filesystem. var http = require(‘http’); var fs = require(‘fs’); http.createServer(function(request, response) { response.writeHead(200); fs.readFile(‘index.html’, function(err, contents) { response.write(contents); response.end(); }); }).listen(8080); Generate Request curl http://localhost:8080
node.js Convert Blocking
Everyone don’t knows why important to write non-blocking programs in Node.js. Here is some code using non-blocking function “readFile()“ Read the contents of ‘index.html’ and log them to the console. var fs = require(‘fs’); fs.readFile(‘index.html’, function(err, contents){ console.log(contents); });