Home Blog Page 9

Digital + Analog Clock using HTML, CSS and JS

0
Building a Digital Analog Clock
Building a Digital + Analog Clock

Hi Welcome to Proto Coders Point, In this tutorial, we’ll building a simple stylish Digital + analog clock using HTML, CSS, and JS. This clock features distinct hour, minute, and second hands will show a numerical indicators where will show current hours, minute & second as a hand of the clock, providing a visually appealing and functional timekeeping display as animation.

Have a look at below gif image how our clock will look

Source Code

<!DOCTYPE html>
<html>
<head>
  <title>Simple Clock with Lines</title>
  <script src="https://code.jquery.com/jquery-3.7.1.js" integrity="sha256-eKhayi8LEQwp4NKxN+CfCh+3qOVUtJn3QNZ0TciWLP4=" crossorigin="anonymous"></script>
  <style>
    #clock {
      position: relative;
      width: 400px;
      height: 400px;
      margin: 50px auto;
      font-size: 18px;
      border:solid
    }
    .hour-line, .minute-line, .second-line {
      position: absolute;
      transform-origin: 50% 100%;
      padding-bottom:15%;
      left:50%;
    }
    .hour-line {
      width: 6px;
      height: 60px;
      top: 90px;
    }
    .minute-line {
      width: 4px;
      height: 80px;
      top: 40px;
    }
    .second-line {
      width: 2px;
      height: 100px;
      top: 25px;
    }
    
  </style>
</head>
<body>

<div id="clock">
  <div class="hour-line" id="hourLine">
    <div class="clock_number">00</div>
    <div class="clock_number">00</div>
    <div class="clock_number">00</div>
    <div class="clock_number">00</div>
  </div>
  <div class="minute-line" id="minuteLine">
    <div class="clock_number">00</div>
    <div class="clock_number">00</div>
    <div class="clock_number">00</div>
    <div class="clock_number">00</div>
    <div class="clock_number">00</div>
    <div class="clock_number">00</div>
    <div class="clock_number">00</div>

  </div>
  <div class="second-line" id="secondLine">
    <div class="clock_number">00</div>
    <div class="clock_number">00</div>
    <div class="clock_number">00</div>
    <div class="clock_number">00</div>
    <div class="clock_number">00</div>
    <div class="clock_number">00</div>
    <div class="clock_number">00</div>
    
  </div>
</div>

<script>
  function updateClock() {
    const now = new Date();
    const hours = now.getHours() % 12 || 12;
    const minutes = now.getMinutes();
    const seconds = now.getSeconds();

    // Calculate angles for hour, minute, and second hands
    const hourAngle = (hours % 12) * 30 + minutes * 0.5;
    const minuteAngle = minutes * 6;
    const secondAngle = seconds * 6;

    // Rotate the lines based on the calculated angles
    $("#hourLine")
     .css("transform", `rotate(${hourAngle}deg)`)
     .children(".clock_number")
     .html(hours)
     .css("transform", `rotate(${-hourAngle}deg)`);

     $("#minuteLine")
  .css("transform", `rotate(${minuteAngle}deg)`)
  .children(".clock_number")
  .html(minutes)
  .css("transform", `rotate(${-minuteAngle}deg)`);    

  $("#secondLine")
  .css("transform", `rotate(${secondAngle}deg)`)
  .children(".clock_number")
  .html(seconds)
  .css("transform", `rotate(${-secondAngle}deg)`);
  }

  // Update the clock every second
  setInterval(updateClock, 1000);

  // Initial call to display the clock immediately
  updateClock();
</script>

</body>
</html>

Above Source Code Explanation

HTML Structure

Let’s start with the HTML structure. The clock is contained within a <div> element with the ID “clock” . Within this div, we have three child div’s which classes named a “hour-line,”minute-line,” and “second-line,”. This 3 div.’s representing the hour, minute, and second hands of the clock, respectively. Additionally, within each line div, there are child div which classes names as “clock_number” to display numerical indicators.


CSS Styling

The CSS code is used to give styling and appearance to the clock design. The clock will have a square shape border, and each hand (hour, minute, and second) is represented by a digital number line with a specific dimensions and positions a the center of the square border. The transform-origin property ensures that the rotation occurs around the base of each line.

JavaScript and jQuery

The JavaScript code is used to update the clock’s hands based on the current time. For this we have a function updateClock that retrieves the current hour, minute, and second and perform calculation for each hand rotations in a proper angle for each hand. The angles we get is then applied using jQuery to rotate the lines. The numerical indicators are updated accordingly.

The setInterval function is used to call the updateClock function every second, ensuring that the clock hands are continuously updated in real-time.

Flutter URL Shortener Project with Source Code

0
Flutter url shortener project

Hi Guy’s Welcome to Proto Coders Point. It this fast growing digital era the importance of concise and shareable URL’s cannot be overstated. If you are trying to make you web address URL manageable or want to share the link of any content on social media or want to embed the URL in a email, but share a URL that is too long is very annoying thing. There for Shortened URL Plays a crucial role.
So In this article we learn how to build a URL Shortener Application in flutter.

How to user flutter URL Shortener

Our Flutter URL Shortener Application we have 3 pages.

  1. Registration.
  2. Login.
  3. Dashboard.

In Registration Page we simply have a form that the user can fill to register himself into our application using Flutter registration page.

In Login page user can enter his login credential and get logged in into the flutter to make use of URL Shortener.

In Dashboard we have 2 section. First section user can use to submit his LongURL and get ShortURL. Second section we can show all the data i.e. short URL created by that user.


Libraries need to created URL Shorten project in flutter

In pubspec.yaml file under dependencies we need to add below 2 library.

  get:
  get_storage:

We are going to make use of getx and get storage package

getx: used to make http calls to back api to store and retrieve data from backend nodejs server.

get storage: used to share logged in user data within the application storage same like sharedPreference.

Flutter URL Shortener Project Structure

url shortener flutter

In above flutter project I have 4 dart files namely:

  1. main.dart
  2. registration.dart
  3. login.dart
  4. dashboard.dart

main.dart

This is the starting point of our flutter application, Here we will check if the user has already signed into the application before, If the user data exist in get_storage (shared preferences) we will redirect the user to dashboard page else will redirect user to login page.

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:url_shortener/dashboard.dart';
import 'package:url_shortener/login.dart';
import 'package:get_storage/get_storage.dart';
void main() async{
  await GetStorage.init();
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  final get_storage = GetStorage();
  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    get_storage.writeIfNull('user', false);

    Future.delayed(Duration(seconds: 2),() async{

      print(get_storage.read('user'));
       checkUserData();
    });


  }

  checkUserData(){
    if(get_storage.read('user').toString().isNotEmpty || get_storage.read('user') ){
      Navigator.push(context, MaterialPageRoute(builder: (builder)=>Dashboard()));
    }else{
      Navigator.push(context, MaterialPageRoute(builder: (builder)=>Login()));
    }
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: CircularProgressIndicator(),
      ),
    );
  }
}

login.dart

In login page we have 2 text field where the user can enter login credential i.e. email & password and a button on click will call a function that hit back-end NodeJS Login API.

After hitting the backEnd API if user exist in our MongoDB database will send the user data as response to frontEnd, and then store the user data into flutter app storage space using GetStorage package so that the used will be signIn into the application when he visit next time.

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:url_shortener/dashboard.dart';
import 'package:url_shortener/registration.dart';
import 'package:get_storage/get_storage.dart';

class Login extends StatefulWidget {
  const Login({super.key});

  @override
  State<Login> createState() => _LoginState();
}

class _LoginState extends State<Login> {

  TextEditingController emailText = TextEditingController();
  TextEditingController passText = TextEditingController();
  final getConnect = GetConnect();
  final get_storage = GetStorage();

  void _login(email,pass) async{

    if(email.isEmpty || pass.isEmpty){
      Get.snackbar("Please Enter Email And Password", '');
    }else{
      final res = await getConnect.post('http://192.168.29.239:3000/userLogin', {
        "email":email,
        "password":pass

      });

      if(res.body['status']){
        get_storage.write('user', res.body['success']);
        Navigator.push(context, MaterialPageRoute(builder: (builder)=>Dashboard()));
      }else{
        Get.snackbar(res.body['success'], '');
      }
    }

  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        body: SingleChildScrollView(
          child: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                SizedBox(height: 10,),
                const Text("* URL SHORTENER *",style: TextStyle(fontSize: 30),),
                const Text("Login Page"),
                SizedBox(height: 10,),
                Padding(
                  padding: const EdgeInsets.fromLTRB(25, 20, 25, 2),
                  child: TextField(
                    controller: emailText,
                    keyboardType: TextInputType.text,
                    decoration: InputDecoration(
                        filled: true,
                        fillColor: Colors.white,
                        hintText: "Email",
                        border: OutlineInputBorder(
                            borderRadius: BorderRadius.all(Radius.circular(10.0)))),
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.fromLTRB(25, 20, 25, 2),
                  child: TextField(
                    controller: passText,
                    keyboardType: TextInputType.text,
                    decoration: InputDecoration(
                        filled: true,
                        fillColor: Colors.white,
                        hintText: "Password",
                        border: OutlineInputBorder(
                            borderRadius: BorderRadius.all(Radius.circular(10.0)))),
                  ),
                ),

                ElevatedButton(
                  onPressed: () {
                      _login(emailText.text,passText.text);
                  },
                  child: Text("LOGIN"),
                ),

                Padding(padding: EdgeInsets.all(20),child:
                ElevatedButton(
                  onPressed: () {
                    Navigator.push(context, MaterialPageRoute(builder: (builder)=>Registration()));
                  },
                  child: Text("REGISTER"),
                ),),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
url shortener login page

registration.dart

Using this page user can register himself into the system.

Here we accept 3 data from the user i.e. Name, Email and Password using Text Fields.

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:url_shortener/login.dart';


class Registration extends StatefulWidget {
  const Registration({super.key});

  @override
  State<Registration> createState() => _RegistrationState();
}

class _RegistrationState extends State<Registration> {

  TextEditingController nameText = TextEditingController();
  TextEditingController emailText = TextEditingController();
  TextEditingController passText = TextEditingController();
  final getConnect = GetConnect();


  void _register(name,email,pass) async{
    print("${name},${email},${pass}");

    if(name.isEmpty || email.isEmpty || pass.isEmpty){
      Get.snackbar("Please Enter Details", '');
    }else{
      final res = await getConnect.post('http://192.168.29.239:3000/userRegistration', {
        "name":name,
        "email":email,
        "password":pass
      });

      if(res.body['status']){
        Navigator.push(context, MaterialPageRoute(builder: (builder)=>Login()));
      }else{
        Get.snackbar(res.body['success'], 'message');
      }
    }

  }
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        body: SingleChildScrollView(
          child: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                SizedBox(height: 10,),
                const Text("* URL SHORTENER *",style: TextStyle(fontSize: 30),),
                const Text("Registration Page"),
                SizedBox(height: 10,),
                Padding(
                  padding: const EdgeInsets.fromLTRB(25, 10, 25, 2),
                  child: TextField(
                    controller: nameText,
                    keyboardType: TextInputType.text,
                    decoration: InputDecoration(
                        filled: true,
                        fillColor: Colors.white,
                        hintText: "Full Name",
                        border: OutlineInputBorder(
                            borderRadius: BorderRadius.all(Radius.circular(10.0)))),
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.fromLTRB(25, 20, 25, 2),
                  child: TextField(
                    controller: emailText,
                    keyboardType: TextInputType.text,
                    decoration: InputDecoration(
                        filled: true,
                        fillColor: Colors.white,
                        hintText: "Email",
                        border: OutlineInputBorder(
                            borderRadius: BorderRadius.all(Radius.circular(10.0)))),
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.fromLTRB(25, 20, 25, 2),
                  child: TextField(
                    controller: passText,
                    keyboardType: TextInputType.text,
                    decoration: InputDecoration(
                        filled: true,
                        fillColor: Colors.white,
                        hintText: "Password",
                        border: OutlineInputBorder(
                            borderRadius: BorderRadius.all(Radius.circular(10.0)))),
                  ),
                ),
                Padding(padding: EdgeInsets.all(20),child:
                ElevatedButton(
                  onPressed: () {
                      _register(nameText.text,emailText.text,passText.text);
                  },
                  child: Text("REGISTER"),
                ),),
                ElevatedButton(
                  onPressed: () {
                    Navigator.push(context, MaterialPageRoute(builder: (builder)=>Login()));
                  },
                  child: Text("LOGIN"),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
url shortener registration page

dashboard.dart

This Page and 2 section

  • First section user can use to submit his LongURL and get ShortURL.
  • Second section we can show all the data i.e. short URL created by that user.
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';

class Dashboard extends StatefulWidget {
  const Dashboard({super.key});

  @override
  State<Dashboard> createState() => _DashboardState();
}

class _DashboardState extends State<Dashboard> {
  final getStoage = GetStorage();
  final textLongUrl = TextEditingController();
  final getConnect = GetConnect();

  List<Map<String,dynamic>> dataList = [];

  void _urlSubmit(longurl) async{

    Map<String,dynamic> userData = getStoage.read('user');
    String userId = userData['_id'];

    final res = await getConnect.post('http://192.168.29.239:3000/urlSubmit',
        {
          'userId': userId,
          'longUrl': longurl
    });

    print(res.body['shortUrl']);

    Get.snackbar("Short URL Created", res.body['shortUrl']);
    textLongUrl.text="";
  }

  Future<void> _fetchUserUrl(userId) async {
  
    final response = await getConnect.post('http://192.168.29.239:3000/getUserURL', {
        'userId' : userId
    });

    final List<dynamic> data = response.body['success'];

    setState(() {
      dataList = List<Map<String,dynamic>>.from(data);
    });
    
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    Map<String,dynamic> userData = getStoage.read('user');
    String userId = userData['_id'];

    _fetchUserUrl(userId);

  }

  @override
  Widget build(BuildContext context) {
    return  Scaffold(
      body: SafeArea(
        child: SingleChildScrollView(
          child: Center(
            child: Container(
              height: MediaQuery.of(context).size.height,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  SizedBox(height: 10,),
                  const Text("* URL SHORTENER *",style: TextStyle(fontSize: 30),),
                  const Text("Long URL to Short URL"),
                  SizedBox(height: 10,),
                  Padding(
                    padding: const EdgeInsets.all(16.0),
                    child: Form(
                      child: Row(
                        children: [
                          Expanded(
                            child: TextFormField(
                                decoration: InputDecoration(
                                  filled: true,
                                  fillColor: Colors.white,
                                  hintText: "Long URL",
                                  border: OutlineInputBorder(
                                      borderRadius: BorderRadius.all(Radius.circular(10.0))
                                  ),
                                ),
                                controller: textLongUrl,
                                validator: (value){
                                  if(value == null || value.isEmpty){
                                    return " Please Enter long Url";
                                  }
                                  return null;
                                }
                            ),
                          ),
                          SizedBox(width: 10), // Add some spacing between TextField and Button
                          ElevatedButton(
                            onPressed: () {
                              _urlSubmit(textLongUrl.text);
                            },
                            child: Text('Submit'),
                          ),
                        ],
                      ),
                    ),
                  ),
                  dataList.isEmpty ? CircularProgressIndicator(): Expanded(
                    child: Container(
                      child: ListView.builder(
                        itemCount: dataList.length,
                          itemBuilder: (context,index){
                          return Card(
                            elevation: 2,
                            margin: EdgeInsets.symmetric(vertical: 6,horizontal: 16),
                            child: ListTile(
                              title: Text("http://192.168.29.239/${dataList[index]['shorturl']}"),
                              subtitle: Text('${dataList[index]['longUrl']}'),
                            ),
                          );
                          }
                      ),
                    ),
                  )
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}
url shortener dashboard page

Video Tutorial on building URL Shortener Project

Below is playlist of my YouTube tutorial on building URL Shortener Application using Flutter for frontend and NodeJS with MongoDB at backend.


URL Shortener BackEnd complete source code

Building a URL Shortener Project with Node.js – Backend

0
nodejs url shortener project
nodejs url shortener project

Introduction

In the world of web application development, building a URL shortener server is best for beginners who wants to learn for scratch and also best for collage student who want to submit a project. A URL shortener project takes long URLs and provides a shortened link, making links more manageable and shareable in short form links. In this article, we will help you in developing a nodejs application in building a URL shortener using Node.js, Express, and MongoDB.

Video Playlist on building URL Shortener in Flutter + NodeJS with MongoDB Database

Project Structure

nodejs url shortener

The NodeJS project will be organized into folders such as config, model, and routers for better code structure.

  1. config: In config folder we have a file db.js that is responsible for mongodb database connectivity.
  2. model: In model folder we have files that define mongodb database schema, Here we have 2 file namely url.model.js and user.model.js.
  3. routers: In router folder we have RESTAPI for url and user apis.
  4. app.js and index.js: This main root / starting point of our nodejs express URL shortener application in nodejs.

Node Module / Libraries required for building URL shortener Application

  1. body-parser: used for parse incoming request bodies from client or front-end.
  2. cors: Enables secure cross-origin resource sharing.
  3. express: used to simplify the creation of nodejs express server and creating API and routers.
  4. mongoose: used to connect or interact with MongoDB database, creating mongodb collection schema.
  5. shortid: used for generating unique random identifier, that we can used as shortURL.

Enter below command to install this nodeJS Modules or Libraries

npm install body-parser cors express mongoose shortid

URL Shortener in NodeJS using ExpressJS and MongoDB

Refer above project structure and create respective files under the folders.

config > db.js

This file is responsible to get connected to mongodb database using nodejs mongoose library.
This connection is exported so that the connection can we used in model file as below.

const mongoose = require('mongoose');

const connection = mongoose.createConnection('mongodb://127.0.0.1:27017/url_shortener')

connection.on('connected',()=>{
    console.log("DB Connected");
})


module.exports = connection;

modul > url.model.js

This is a MongoDB schema for url collection, where we are going to store user submitted URL’s data like userId, longUrl and shorturl.

const mongoose = require('mongoose');
const db = require('../config/db');
const userModel = require('./user.model')

const { Schema } = mongoose;

const urlSubmitionSchema = new Schema({
    userId:{
        type: Schema.Types.ObjectId,
        ref: userModel.modelName
    },
    longUrl:{
        type : String
    },
    shorturl:{
        type : String
    }
});

const urlSubmitionModel = db.model('urlSubmittion',urlSubmitionSchema);
module.exports = urlSubmitionModel;

modul > user.model.js

This is a MongoDB schema for user collection, where we are going to store users data like name, email and password.

const mongoose = require('mongoose');
const db = require('../config/db');

const { Schema } = mongoose;

const userSchema = new Schema({
    name:{
        type:String
    },
    email:{
        type:String
    },
    password:{
        type:String
    },
})

const userModel = db.model('users',userSchema);
module.exports = userModel;

routers > url.router.js

This use url routers has 3 API:

  1. urlSubmit: Using this we can convert long URL into shorturl, Basically this API is used for LongURL submition and get shortURL of it.
    This API accept 2 parameters: userId & longUrl then generate a shortId i.e a random id which we can use as shortURL. This whole data will be stored into Mongodb database.
  2. /:url: this api is a parameterized API, for example support a longURL is submitted and uses get the shortURL it, Now the user can us url like this http://localhost:3000/xya12id to redirect himself to longURL.
  3. getUserURLList: This API will return all the URL’s been submitted by the user, This is been used to show or display the list of URL in Frontend (URL Shortener Flutter App).
const router = require('express').Router();
const shortId = require('shortid');
const urlSubmitModel = require("../model/url.model");

router.post('/urlSubmit', async (req, res) => {
    const { userId, longUrl } = req.body;
    try {
        const randonurl = shortId.generate();
        const urlSubmit = new urlSubmitModel({ userId, longUrl, shorturl: randonurl });
        await urlSubmit.save();

        res.json({ status: true, shortUrl: `http://localhost:3000/${randonurl}` })
    } catch (error) {
        res.json({ status: false, message: "Something went wrong" })
    }
});

router.get('/:url',async(req,res)=>{
   const {url} = req.params;

   const urldata = await urlSubmitModel.findOne({shorturl:url})

   console.log(urldata);

   if(urldata){
            res.redirect(urldata.longUrl);
   }else{
    res.json({status:false,message:"invalid url"})
   }

});

router.post('/getUserURLList',async(req,res)=>{
    const {userId} = req.body;
    const allUserUrl = await urlSubmitModel.find({userId});
    if(allUserUrl){
        res.json({status:true,success:allUserUrl})
    }else{
        res.json({status:false,message:"no data found"});
    }
})



module.exports = router;

routers > user.routers.js

In user API routers we will have 2 API for users

  1. userRegistration: using this we can register user to our system.
  2. userLogin: Using this the user can use his credential or login detail to Login into our URL Shortener Flutter Application.
const router = require('express').Router();
const userModel = require('../model/user.model');


router.post('/userRegistration', async (req,res)=>{
    try {
        const {name,email,password} = req.body;

        const user = await userModel.findOne({email});
        if(!user){
            const createuser = new userModel({name,email,password});
            await createuser.save();


            res.json({status:true,success : "User Created Successfully"});
        }else{
            res.json({status:false,success : "User Exist"});
        }

    } catch (error) {
        console.log(error);
    }
   

});



router.post('/userLogin', async (req,res)=>{
    try {
        const {email,password} = req.body;

        const user = await userModel.findOne({email,password});
        if(user){
           
            res.json({status:true,success : user});
        }else{
            res.json({status:false,success : "User Invalid Please Try Again"});
        }

    } catch (error) {
        console.log(error);
    }
});

module.exports = router;


app.js

The app.js file is where we will define our middleware like cors, routers using express, and is exported using module.exports and imported in index.js

const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const userRouter = require('./routers/user.router');
const urlRouter = require('./routers/url.router');
const app = express();

app.use(cors());

app.use(bodyParser.json({limit:'50Mb'}));
app.use(bodyParser.urlencoded({limit:'50Mb',extended:true}))

app.use('/',userRouter);
app.use('/',urlRouter);

module.exports = app;

index.js

This file is where our NodeJS Express Server start.

const app = require("./app")


app.get('/',(req,res)=>{
    const { data } = req.body;
   res.send(` Hello ${data}`)
})

app.listen(3000,()=>{
    console.log("Localhost Express running at 3000");
})

How to run url shortener backend nodejs server

node index.js

Flutter URL Shortener Complete Source Code

NodeJS Allow whitelisted IP to use Express Server

0
White Listing IP in NodeJS Express Server
White Listing IP in NodeJS Express Server

Node.JS as you know is single threaded, asynchronous, event-driven architecture, Is so popular JavaScript JavaScript runtime environment for building high performance web application or back-end application like express server or Rest API’s.

You are here to know how can I increase the security level of my NodeJS express server by whitelisting IP to use our node server.

In this NodeJS article let’s explore how to enhance security of our NodeJS Express by adding middleware to check the whitelisted IP and allow only specific Client IP address to access our server.

Video Tutorial


The code complete source code with explanation is as below

Allow Only one specific IP

index.js

const express = require('express')

const app = express();

const checkIpAccess = (req,res,next)=>{
    const ALLOWEDID = "192.168.29.78";
    const CLIENTIP = req.ip;

    
    if(CLIENTIP === ALLOWEDID || CLIENTIP === "::ffff:"+ALLOWEDID){
      next();
    }else{
      res.status(403).send("Forbidden");
    }
    
}

app.use(checkIpAccess);

app.get('/',(req,res)=>{
  res.send(`ALLOWED ACCESS  ${req.ip}`);
})

app.listen(3000,()=>{
  console.log("Server Running at port 3000");
})

Whitelisting multiple IP’s in NodeJS

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

const checkIpAccess = (req, res, next) => {
    const ALLOWEDIPS = ["192.168.29.78", "192.168.29.79", "192.168.29.80"];
    const CLIENTIP = req.ip;

    if (ALLOWEDIPS.includes(CLIENTIP) || ALLOWEDIPS.includes("::ffff:" + CLIENTIP)) {
        next();
    } else {
        res.status(403).send("Forbidden");
    }
}

app.use(checkIpAccess);

app.get('/', (req, res) => {
    res.send(`ALLOWED ACCESS  ${req.ip}`);
})

app.listen(3000, () => {
    console.log("Server Running at port 3000");
})

Understanding the above code

The above code make use of ExpressJS to run a NodeJS server, which makes use of middleware function checkIpAccess thta is responsible to check the client request IP address if that IP exist in our allowed whitelisted IP list then grant the user to use our nodejs server else return a status error 403 that is forbidden.

Benefits of IP Whitelisting

Implementing IP whitelisting in your Node.js Express server offers several security advantages:

  1. Access Control: Only requests originating from the specified IP address are permitted, providing a level of access control.
  2. Reduced Attack Surface: By restricting access to a single IP, you minimize the potential attack surface, making it more challenging for unauthorized users to interact with your server.
  3. Enhanced Security: IP whitelisting is an additional layer of security, complementing other security measures you may have in place.

JavaScript: Solutions to 5 Essential Interview Questions

0
javascript interview questions
JS questions

In today’s world JavaScript versatile & been widely-used as a programming language that plays an important role in modern web development. Being a JavaScript developer, It’s very important to understand the language’s fundamentals and also adept solving real-world problems. In JS job interviews, JavaScript Interview Questions is been often asked from basic syntax to advanced concepts.

In this article, We will dive into 11 JavaScript questions that are commonly asked in interviews. Each question will be explained in detailed with solution, complete with source code examples where required.

If are you getting prepared for you next job interview or simple learning to enhance your JS skill, this article will have practical solution with code example for better understanding.

5 Interview Questions for JavaScript Job preparation

1. Implement a function that serializes a JavaScript value into a JSON string.

In JS we have a In-Built method i.e. JSON.stringify() using which we can easily convert javascript object or value into JSON string.

function serializeToJsonString(value) {
  try {
    const jsonString = JSON.stringify(value);
    return jsonString;
  } catch (error) {
    console.error('Serialization error:', error);
    return null;
  }
}

// Example JS Object:
const data = {
  name: 'John Doe',
  age: 30,
  city: 'Example City'
};

const jsonString = serializeToJsonString(data);
console.log(jsonString);

In Above example we have a function which accept a data object and return a jsonString.


2. Write a JS function which performs a deep copy of a value, and also handles circular references.

Before going in above question let’s understand what is shallow copy and deep copy.

Shallow and Deep copy are concept in JS which is been used while working with object or array in javascipt:

Shallow Copy in JavaScript:

In below source code, I am creating a copy of originalObject using Assignment Operator (=) and then the copyed object shallowCopy is modified…

const originalObject = { a: 1, b: { c: 2 } };

// Shallow Copy using assignment operator
const shallowCopy =  originalObject;

console.log('Original Object: Before', originalObject);
console.log('Shallow Copy: Before', shallowCopy);

// Modifying the Shallow Copy
shallowCopy.b.c = 99;

console.log('Original Object:', originalObject);
console.log('Shallow Copy:', shallowCopy);

In Shallow Copy when you create a new object using originalObject, actually the shallowCopy points to same address to which originalObject is, Now when we modify shallowCopy the modification is made at address level, due to which originalObject will also get altered.

// OUTPUT
Original Object: Before { a: 1, b: { c: 2 } }
Shallow Copy: Before { a: 1, b: { c: 2 } }
__________________________________________
Original Object: { a: 1, b: { c: 99 } }
Shallow Copy: { a: 1, b: { c: 99 } }

Deep Copy in JavaScript:

In below example, JSON.stringify() is used to serialize the original object into a JSON string, and then JSON.parse() is used to parse it back into a new object.

Doing this will create a deep copy, making sure that original data is not modified when copy is modified.

// Original Object
const originalObject = { a: 1, b: { c: 2 } };

// Deep Copy using JSON.stringify() and JSON.parse()
const deepCopy = JSON.parse(JSON.stringify(originalObject));

// Modifying the Deep Copy
deepCopy.b.c = 99;

console.log('Original Object:', originalObject);
console.log('Deep Copy:', deepCopy);

Now, Let’s performs a deep copy of a value, and also handles circular references

Note: We are using NodeJS circular-json library to handle circular references with deep clone.

const CircularJSON = require('circular-json');

function deepCopyWithCircularHandling(obj) {
  try {
    const jsonString = CircularJSON.stringify(obj);
    const copy = CircularJSON.parse(jsonString);
    return copy;
  } catch (error) {
    console.error('Deep copy error:', error);
    return null;
  }
}

// Example usage:
const originalObject = {
  prop1: 'value1',
  prop2: {
    prop3: 'value3'
  }
};

originalObject.circularReference = originalObject;

const copiedObject = deepCopyWithCircularHandling(originalObject);
console.log(copiedObject);

In this example, the circular-json library is used to stringify and parse the object with circular references.


3. Create a Function in JS then accepts any numbers of arguments and returned memorized version

To Implement Memorization of data or we can say it as creating a cache of the data returned by a javascript function can be achieved by using caching technique.
Below Code example uses simple cache object.

function memoize(func) {
  const cache = {};

  return function (...args) {
    const key = JSON.stringify(args);

    if (cache[key]) {
      console.log('Fetching from cache');
      return cache[key];
    } else {
      console.log('Calculating result');
      const result = func(...args);
      cache[key] = result;
      return result;
    }
  };
}

// Example usage:
const sum = memoize((...args) => args.reduce((acc, val) => acc + val, 0));

console.log(sum(1, 2, 3)); // Calculating result
console.log(sum(1, 2, 3)); // Fetching from cache

4. How do you stop a setInterval() function

We can easily stop setInterval() function of javascript, by making use of clearInterval(intervalId) method. The clearInterval method accept a parameter i.e. the ID of setInterval() method by which be can stop further executions of setInterval() func.

var i = 0;
const intervalId = setInterval(()=>{
    
    console.log("i=> ",i)
    i++;
    if(i===5){
        clearInterval(intervalId)
    }
},1000);

The above code will stop execution of setInterval() when i value is 5.


5. How to Merge Two Object in JavaScript

There are many ways to merge or cancat two object into one, i.e. by using spread operator (...) or by uisng Object.assign() method.

The below example to merge two object using spread operator (...).

function mergeObjects(obj1, obj2) {
  return { ...obj1, ...obj2 };
}

// Example usage:
const obj1 = { a: 1, b: 2 };
const obj2 = { c: 4 };

const mergedObject = mergeObjects(obj1, obj2);
console.log(mergedObject); // result: { a: 1, b: 2, c: 4 }

The below example to merge two object using Object.assign() method.

function mergeObjects(obj1, obj2) {
  // Create a new object and use Object.assign to merge obj1 and obj2
  const mergedObject = Object.assign({}, obj1, obj2);
  return mergedObject;
}

// Example usage:
const obj1 = { a: 1, b: 2 };
const obj2 = { b: 3, c: 4 };

const mergedObject = mergeObjects(obj1, obj2);
console.log(mergedObject); // result: { a: 1, b: 3, c: 4 }

JavaScript for…in Loop: Iterating an object

0
for…in object javascript

Hi Guy’s Welcome to Proto Coders Point, In this JS Article let’s under about looking in javascript using for…in loop.

for…in object javascript -How to iterate object in javascript

In Javascript, for…in loop statement is commonly used to iterate object over it’s properties also known a key of an object. This for…in loop is only recommended to used with object not with any array as the behavior might changed during iteration.

Syntax of for…in object loop

for (var key in object) {
    // code to be executed for each property/key
}

for…in loop example to iterate object to access it’s properties

const person = {
  firstName: "Rajat",
  lastName: "Palankar",
  age: 20,
  email: "contact@protocoderspoint.com"
};

for (var key in person) {
  console.log(key + ": " + person[key]);
}

Output

In above code we have an object by name person & I want to iterate it so for that I am using for…in loop to iterate the object over it’s properties.

Each loop it iterate has it’s key property and using that key we can access it’s corresponding value. The output is below:

firstName: Rajat
lastName: Palankar
age: 20
email: contact@protocoderspoint.com

Note: Use for…in loop use only on object, as it misbehave which used with arrays. For iterating an arrays it’s better to use for…of loop or forEach loop.

Similar Articles

convert array into collection

15 useful Javascript Snippets code