Posts

Showing posts from February, 2023

How to use others API in my website?

 To Use others API to get data, first thing you need is API_KEY. Some API's are :     To get random quotes :   https://api.kanye.rest           { " quote ":  "I don't expect to be understood at all."      }           To get random Jokes  :  h ttps://v2.jokeapi.dev/joke/Any            ENDPOINT - >    https://v2.jokeapi.dev/joke / this is an endpoint for api.      To use Weather details in your website : Openweather API app . get ( "/" , function ( req , res ){     const weatherUrl = "https://api.openweathermap.org/data/2.5/weather?                                    q=Dhangadhi&appid={api_key}&units=metric" ;     https . get ( weatherUrl , function ( response ){   ...

calculator app using node express

Image
 Calculator App Q. How to get form data in express server? Ans: Use Body-Parser Module to get html form data into the server.      Example:  app . get ( "/" , function ( req , res ){     res . sendFile ( __dirname + "/index.html" ); }); app . post ( "/" , function ( req , res ){     res . send ( "Data sent !" ); }); If we wanna try to get from data we get form values in PayLoad section in Chrome Developer Tool                    Here we can see :                    firstNumber : 2                    secondNumber: 6                  these numbers are bound to forms input fields To use these information in server We Need BodyParser     > npm install body-parser Then include it in your code:     > con...

Understanding and working with Express Route

 Routes in Express Here is an example how we create multiple routes in express : const express = require ( "express" ); const app = express (); const PORT = 3000 ; app . get ( "/" , function ( req , res ){     res . send ( "<h1>Hello Express</h1>" ) }); app . get ( "/contact" , function ( req , res ){     res . send ( "Contact : <b>mayacr62@gmail.com</b>" ); }); app . get ( "/about" , function ( req , res ){     res . send ( "<h3>Hello, I am Mayaram Chaudhary. Click Me <a href='github.com/maya-cx'>GitHub</a>" ); }) app . listen ( PORT , function (){     console . log ( "Server started on port " + PORT ); }); Struggling refreshing page everytime ? Install NodeMon package which monitors the changes in you source and restart server automatically. > npm install nodemon To start nodemon > nodemon file.js and hit enter

Setup node framework | Express

Image
 First step to start with express is to install express in our project.     >  npm install express  This command will add express dependency into package.json file and download packages from internet. Let's create a new express server: const express = require ( "express" ); const app = express (); const PORT = 3000 ; app . listen ( PORT , function (){     console . log ( "Server started on port " + PORT ); });     Here is an error Cannot GET / becoz we do set the request or response from the server: app . get ( "/" , function ( request , response ){     console . log ( request ); }); We can see now all the request parameters in console. To send the response to the client :  app . get ( "/" , function ( req , res ){     res . send ( "Hello Express..." ); }); Now we can see the output that is a response from the server on / location. You can also send HTML as a response :  app . get ( "...

Node Js Setup and using built-in and external modules

Image
 Node JS 1. Use of Built-in Modules      const anyName = require(' module_name ');     Example : How to copy a file Using File System in Node Javascript.     Solution : Suppose we have a file namely file1.txt and they contain some text in it.                     const fs = require(" fs "); //includes file system module                     fs.copyFileSync("file1.txt", "file2.txt"); //copyFileSync is method to copy file                         //here file2.txt will be created with the text that inside file1.txt and if file already                                    exists it will be replaced           2. U...