Vaidikalaya

First Program In Node


Node helps to write front-end and back-end code in the same language. It helps to write efficient code for real-time applications.

In Node.js, there are multiple approaches and styles you can use to write your code. But as a beginner in development, We will discuss the following two approaches to create our first program in Node:

Console Base

Web Base


Creating Console-based Program

In a console-based approach, we write our Node.js code in a .js file and execute this file using the command line interface (CLI), such as Command Prompt (CMD) on Windows, Terminal on macOS, or a shell on Linux. This method involves interacting with the program directly through the console, where inputs are taken and outputs are displayed.

Steps to create:

Step 1: Open Visual Studio Code on your computer create a new file 'app.js' and write the code below.

console.log('Hello Vaidikalaya');

Now save this file.

Step 2: Open the Integrated Terminal in VS Code and Run this file using the following command and press Enter

node app.js
Output

Hello Vaidikalaya

Screenshot


When to use a console-based approach

⮚ Console-based applications are simpler and less complex compared to web applications. They help beginners understand basic programming concepts without the overhead of dealing with web servers, client-server communication, and front-end technologies.

⮚ It allows developers to focus on learning the core language features and logic without distractions.

⮚ Developers can easily output values and states of variables to the console for debugging purposes.

⮚ When writing backend code, many scripts run on the server without a user interface, making console-based approaches essential.

⮚ Scripts for deployment, database migrations, and other server-side operations are often written to be run from the command line.

⮚ Console-based Node.js applications are easily integrated into Continuous Integration/Continuous Deployment (CI/CD) pipelines.

⮚ Many Node.js packages are command-line tools that help automate development workflows, such as webpack, eslint, and npm.


Creating Web Base Program

In the web-based approach, we create a web server that handles HTTP requests and serves responses to clients (such as web browsers). This approach is ideal for building dynamic web applications, APIs, and services. Here’s a simple example of creating a web server using Node.js:

Steps to create:

Step 1: Open Visual Studio Code on your computer create a new file 'app.js' and write the code below.

var http=require('http');
http.createServer(function(req,res){
    console.log('Server Start...')
    res.write("Hello Vaidikalaya");
    res.end();
}).listen(5000);

Now save this file.

Step 2: Open the Integrated Terminal in VS Code and Run this file using the following command and press Enter

node app.js

Screenshot

Step 3: Open your web browser and navigate to http://localhost:5000 to see the output.



When to use a web-based approach

⮚ When you need to create interactive user interfaces that can be accessed via web browsers.

⮚ When developing SPAs where the frontend and backend interact through APIs.

⮚ When developing real-time chat applications or implementing features like live notifications, and real-time analytics.

⮚ When managing user authentication and authorization processes.