How to start coding Node.js in 60 minutes

How to start coding NodeJs in 60 minutes

Table of Contents

Node.js is a revolutionary open-source JavaScript runtime environment built on Chrome’s V8 JavaScript engine. I’m calling it revolutionary because it changed the way we are using Javascript nowadays. Javascript is not a language that is only running on browsers anymore. Today, Javascript is highly preferred for developing platform-independent applications. It goes far beyond making websites just interactive. Node.js is an absolute touchstone for Javascript language. 

  • Node.js is event-driven by definition.
  • Node.js has a non-blocking I/O model, preventing threads from waiting for I/O operations to complete and bringing efficiency.
  • npm is the most extensive package registry in the world by having over 350,000 packages. This number doubles the Apache Maven repository.
  • Node.js is asynchronous by birth since Javascript is.
  • You can use Node.js for  Data Streaming, Real-Time Applications, Server-Side Proxy, web UI, scripting, and automation.

Using NodeJS eventually will increase productivity as it uses the same programming language for both back-end and front-end. 

Image Source: https://www.simform.com/nodejs-use-case/
Image Source: https://www.simform.com/nodejs-use-case/

How to install Node.js?

Download the installer from nodejs.org and run it. Follow the instructed steps to finalize the installation.

Verify the installation;

				
					% node --version
v16.13.0
				
			

How to create an HTTP server with Node.js?

Creating an HTTP server is a straightforward task in Node.js; the code block below will create a simple server running on port 8080.

				
					var http = require("http");

http.createServer(function (request, response) {
   response.writeHead(200, {"Content-Type": "text/plain"});
   response.end("Hello, World!");
}).listen(8080);

console.log("Server is running at http://localhost:8080");
				
			

Save this in a file called my-first-nodejs-app.js and run with the command;

				
					% node my-first-nodejs-app.js
Server is running at http://localhost:8080

				
			

validate HTTP server on the browser http://localhost:8080/

Output;

Hello, World!

How to write files with Node.js?

Let’s add some interactivity to our node application. First, update the code to save a pre-defined dataset in a file called exceptionly.json to your local storage system when the server is started. 

To save data in JSON format, we must create a JSON string of the data with JSON.stringify that will return JSON string representation of a JavaScript object which can be written to a file then.

				
					var http = require("http");
const fs = require("fs");

http.createServer(function (request, response) {

  let exceptionly = { 
       site: "https://exceptionly.com", 
       topic: "How to start coding Node.js in 60 minutes" 
   };
   let data = JSON.stringify(exceptionly);
   fs.writeFileSync("exceptionly.json", data);


   response.writeHead(200, {"Content-Type": "text/plain"});
   response.end("exceptionly.json file is saved to the local storage");
}).listen(8080);

console.log("Server is running at http://localhost:8080");

				
			

You can access the physical file system with the fs module and do IO operations easily. But, first, stop and start the server to observe the file creation.

How to read files with Node.js?

I’m extending the code sample to write pre-defined data to a file called exceptionly.json and then read the same file asynchronously with the readFile function from the fs module. 

				
					var http = require("http");
const fs = require("fs");

http.createServer(function (request, response) {

  let exceptionly = { 
       site: "https://exceptionly.com", 
       topic: "How to start coding Node.js in 60 minutes" 
   };
   let data = JSON.stringify(exceptionly);
   fs.writeFileSync("exceptionly.json", data);

   fs.readFile('exceptionly.json', (err, data) => {
       if (err) throw err;
       let result = JSON.parse(data);
       response.writeHead(200, {"Content-Type": "text/plain"});
       response.write(JSON.stringify(result));
       response.end("\n Reading file is done!");
   });

}).listen(8080);

console.log("Server is running at http://localhost:8080");

				
			

Node.js will create a file, and it will read the same file in the same code block if you restart the server. Navigate to localhost:8080 to see the results. 

				
					{"site":"https://exceptionly.com","topic":"How to start coding Node.js in 60 minutes"}
 Reading file is done!

				
			

nodejs.org has some great “How to” articles you may want to look at it. 

I want to highlight the node modules problem as it creates many files and folders, even for a simple CLI project with a few js scripts. This topic is under discussion (and was) and already got too many posts & questions, which clearly shows the number of files and their necessity.

At this point, I will leave you with one of my favorite images that points out the problem 🙂

Image source: https://dev.to/leoat12/the-nodemodules-problem-29dc

3 Responses

  1. Pingback: Exceptionly

Leave a Reply

Your email address will not be published. Required fields are marked *

en_USEnglish