Menu BAR

FEEL FREE TO ASK ANY PROGRAM and please mention any error if you find it
Showing posts with label nodejs. Show all posts
Showing posts with label nodejs. Show all posts

12 Feb 2024

NODE JS PROGRAM - PRINT NUMBERS FROM 1 TO 10


## Write a Node Js program to print numbers from 1 to 10



// Define the upper limit for the loop
const limit = 10;

// Print numbers from 1 to the limit using a for-loop

console.log("Printing numbers using a for loop:");
for (let i = 1; i <= limit; i++) { 
                  console.log(i);
}

11 Feb 2024

NODE JS PROGRAM - FACTORIAL OF A NUMBER


 ## Write a Node Js program to print the Factorial of a number




function factorial(n) { 
            if (n === 0 || n === 1) { 
                                return 1; 
            
            else { 
                                return n * factorial(n - 1); 
            


const number = 10;                             // Change this to calculate the factorial of a different number 
const result = factorial(number);       // Function calling

console.log(`Factorial of ${number} is: ${result}`);

NODE JS PROGRAM - PRINT EACH NUMBER OF AN ARRAY USING LOOP


 ## Write a Node Js program to iterate over an array and print each number




const arrNumber  = [1,2,3,4,5];

console.log ( "Printing numbers using a FOR Loop: "); 

for (let i = 0 ; i < arrNumber.length ; i++) {
            console.log (arrNumber[i]);
}

1 Jan 2024

Node js program - IF ELSE IF example

# Node js program using if else if statement

var a = 'route 1';

if (a === 'route 1'){
    console.log ('Route is 1');
}else if ( a === 'route 2')  {
     console.log ('Route is 2');
}else {
    console.log ( 'No Route');
}


## When you run this program
## Output will be - 
Route is 1

Node js program - IF ELSE example

# Node js program using if else statement

var a = 'route 1';

if (a === 'route 1'){
    console.log ('True');
}else {
     console.log ('False');
}


## When you run this program
## Output will be - 
True

28 Dec 2023

Node js to create the mysql Database

var mysql = require('mysql');
var con = mysql.createConnection({ host: "localhost", user: "admin", password: "admin" });
con.connect(function(err) {
    if (err)
        throw err;
    console.log("Connected!");
    con.query("CREATE DATABASE cprogramcodes", function (err, result) {
         if (err)
            throw err;
        console.log("Database created");
     });
});



21 Dec 2023

Node js program to write HELLO WORLD

 // node js code to create a server and write HELLO WORLD!

var http = require ('http');

http.createServer (function (req,res) => {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('HELLO WORLD!');})
.listen(8080);


// Copy and paste the above code into a file and name it helloworld.js
// Now to run it, open the command prompt and go to the file location
// Execute the first command - >
//                                                 npm i http
//Execute the second command ->
//                                                node helloworld.js
// now open browser and copy and paste the link in the URL - http://localhost:8080

15 Nov 2022

Node JS program to connect to SQL table and use WHERE clause

  ## Node JS program to connect to SQL table and use WHERE clause ## 

var mysql = require ('mysql');

// create a connection 
var con = mysql.createConnection({
    host: "localhost",
    user: "admin",
    password: "admin",
    database: "dummy"
});

con.connect(function(err)){
    if (err) throw err;

    con.query("Select * from student where age>15", function (err, result, fields) {
    if (err) throw err;
   
    console.log (" Result is: ", result);
    });
});

14 Nov 2022

Node JS program to delete a text file

 ## Node JS program to delete a text file ##


var fs = require ('fs');


fs.unlink ('file.txt', function (err){
    if (err) throw err;

    console.log (' File deleted successfully!! ');
});


===OUTPUT===

Run the program using the node command:
$ node deleteFile.js
File deleted successfully!! 

Node JS program to connect to sql server

## Node js program to connect to SQL server ##

var express = require ('express');
var app = express();

app.get ('/', function (req,res){
    var sql = require ("mssql");

    // database config
    var config = {
        user: 'admin',
        password: 'admin',
        server: 'localhost',
        database: 'db'
    };

    // Database connectivity
    sql.connect (config, function (err){
        if (err) console.log (err);
        
        //create a request object
        var request = new sql.Request();

        // database query
        request.query (' Select * from student', function (err, record){
        res.send (record);
        });
    });
});

var server = app.listen(3000, function (){
    console.log (' Server running ....');
});

Node JS program to read a text file

 4) Program to read a file from node.js

const fs = require ('fs');

fs.readFile('./test.txt','utf8', (err, data) => {
    if (err) {console.error(err); }

    console.log ("Data is: "+data);
});

13 Nov 2022

Node JS program to create a new file

 3) Create a new text file from Node.js 

var fs = require ('fs');  // include pre-built node file stream module
var data = "Creating a new file";


fs.writeFile('newFile.txt', data, function (err){
    if (err) throw err;
    console.log (" File created successfully"};
});


Run the program in the terminal:
$ node createNewFile.js
File created successfully

Node JS program to create a module

2) Program to create a node module

Let us create a Calculator.js file with add, subtract, and multiply functions ->

// Addition of two numbers
exports.add = function (x, y){
    return x+y;
}

// Subtraction of two numbers
exports.subtract = function (x, y){
    return x-y;
}

// Multiply of two numbers
exports.multiply = function (x,y){
    return x*y;
}

Let us create a new node file to call these functions ->

var calc = require ('./Calculator.js')
var x = 15, y= 10;

console.log (" Addition of two numbers is: "+ calc.add (x,y));
console.log (" Subtraction of two numbers is: "+ calc.subtract (x,y));
console.log (" Multiplication of two numbers is: "+ calc.multiply (x,y));


===OUTPUT===

Addition of two numbers is: 25
Subtraction of two numbers is: 5
Multiplication of two numbers is: 150

Node JS program to create a server

## Program to create a server in node.js language ##


var http = require ('http');
http.createServer( function (req, res) {
    res.writeHead (200, {'Content-type': 'text/plain'});
    res.end ('Hello World!!');
}).listen (8080);

===OUTPUT===

Hello World!!