Showing posts with label Node. Show all posts
Showing posts with label Node. Show all posts

June 11, 2022

Configure a API Backend Server with custom domain on Amazon Web Services (AWS) EC2 Ubuntu with NGINX and Certbot SSL

Hi all,

Build your own API backend server on AWS free-tier with SSL.

The below steps with reference links will provide detailed information of each step.

  1. AWS account - Free-tier
  2. Create EC2 Ubuntu
  3. Connect to Ubuntu from your local using SSH/Putty
  4. Install NodeJS
    1. Open terminal
    2. curl -fsSL http://deb.nodesource.com/setup_lts.x | sudo -E bash -
    3. sudo apt update
    4. sudo apt install nodejs //installs latest LTS nodejs
  5. Create a sample node server
  6. Install and configure node server process manager
    1. sudo npm install -g pm2@latest
    2. Navigate to nodejs server folder
    3. pm2 start <filename>
    4. pm2 startup systemd
    5. pm2 save
    6. pm2 list // List all the nodejs apps with status
    7. pm2 stop/restart/start app_name/id // Actions of nodejs servers
  7. Install Nginx
  8. Configure your domain DNS records to map to AWS EC2 public/elastic IP address
  9. Secure Nginx with SSL certs

Hope the above information is useful to set up the environment and play with your backend server.

Feel free to send your feedback and comments to psrdotcom@gmail.com


April 29, 2022

Publish and Consume NPM Packages using GitHub Package Manager

 Hi folks,

We are used to publishing the npm packages on npmjs.com. Today, I will show how we can publish the npm packages to GitHub and the steps to use those packages.

1. Publishing Packages

Prerequisites

We need to have an account with GitHub

JavaScript native/framework based project with package.json

Add the .npmrc file to your project with the following syntax. Change the OWNER value to your GitHub account or organization name.

@OWNER:registry=https://npm.pkg.github.com

Publishing Process to GitHub

Create personal access token using the following steps

  • Login to GitHub
  • Go to Settings
  • Navigate to "Developer Settings" -> "personal access tokens"
  • Generate token with write:packages access permissions

  • Open terminal in the project folder
  • Execute the command to login
  • npm login --scope=@OWNER --registry=https://npm.pkg.github.com
  • When prompted,
    • Username: GitHub username
    • Password: Personal access token
    • Public e-mail: Your email address
  • Enter the following command to publish the package
  • npm publish

2. Consume/Installing the packages

Prerequisites

We need to have an account with GitHub

Installation

Add the .npmrc file to your project with the following syntax. Change the OWNER value to your GitHub author or organization name.

@OWNER:registry=https://npm.pkg.github.com

Enter the full name @OWNER/package-name-version in dependencies in package.json
Install the dependencies
npm install

Note: If the organization is asking for authorization of personal access token, then you need to authorize from the personal access token section against the token for the respective organization.

Hope you will make use of GitHub for publishing the packages and consuming them with ease.

Request you to send your feedback and comments to psrdotcom@gmail.com

February 02, 2022

Ubuntu - update NPM and NodeJS

Hi folks,

Some people often confuses with NPM update vs Node.js update.

Let me clarify that, NPM (Node Package Manager) is a CLI (Command Line Interface) to manage the JS (JavaScript) packages from https://www.npmjs.com/ or any other external package managers like GitHub.

Node.js is a FrontEnd JavaScript Framework like Vue.js, AngularJS which helps to build the scalable network applications. Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.

NVM (Node Version Manager) and n are command line utilities to manage and switch between Node.js versions.

When you are installed for the first time, you will be installing Node.js from https://nodejs.org/en/download/ which also installs NPM CLI utility.


But when upgrading the NPM or Node.js, it's a different story.


Update NPM

npm install -g npm

Update Node.js with Node version manager (n)

Install the n

sudo npm install -g n

Update the Node.js to stable version

sudo n stable

As of now, the stable version is 16.3.2 which includes NPM 8.1.2

Update the Node.js to current version

sudo n current

As of now, the current version is 17.4.0 with NPM 8.3.1


Check the versions

NPM

npm --version

Node.js

node --version


Hope this helps you to understand the NPM and Node.js difference along with update of individual components.

Send your comments and feedback to psrdotcom@gmail.com

 

March 03, 2020

Run local file server with NPM http-server

Hi folks,

Today i'll explian the process of making your folder as a standalone file server.

Objective

Make my folder contents browse over http

Procedure


  • Install Node
  • Install http-server module in node globally
    • npm install -g http-server
  • Open terminal/command-prompt
  • Navigate to your folder
  • Run the following command to start the server
    • http-server .
  • or simply run
    • hs .
  • Run from anywhere with full path of the folder
    • hs C:\FileFolder
  • Starting up http-server, serving .
  • Available on:
    • http://10.10.6.2:8080
    • http://127.0.0.1:8080
    • http://youripaddress:8080
  • Hit CTRL-C to stop the server
  • You should be able to browse your file folder contents with any of the above URLs
Hope you are able to access your file contents over the browser

Tip: Using this approach, you can share your content over intranet/internet with full control over data as read only.

After stopped the server, access to the folder is stopped.

Please send your comments and feedback to psrdotcom@gmail.com

February 21, 2019

node server address in use error resolution for Windows and Linux machines

Hi friends,

Today, I have come across a situation, where my node server existed unproperly and address has been blocked. Because of which, I am unable to run the node server on the same port.

System configuration

Running my node server on port 3000.

Resolution

Windows Machine

We will first try to find whether the port has been still in listening mode.
netstat -ano | find "LISTENING" | find "3000"
When the above command returns result then take the last number (Process ID)
We will kill the process with the following command.
> taskkill /f /pid

Linux Machine

> netstat -nlp | grep :3000
> kill -9

Now, you will be able to run the node server on the same port which has been blocked.

Please send your comments and feedback to psrdotcom@gmai.com

April 29, 2012

Huffman Code in C/C++ with String as Node

Hi everyone,

I have copied someone's code and modified. But Since, the author has put the code as open source, I have done that for my friend.

Here it goes, Huffman code in C/C++

Source Code:
#include<conio.h>
#include<string.h>
#include<math.h>
#include<iostream.h>
#include<stdlib.h>

#define ARRAY_SIZE 20
#define MAX_NAME_LEN 25

int g,h,p,y,n;
char m[ARRAY_SIZE],b[ARRAY_SIZE][2],re;

int alpha = 65;

char name[ARRAY_SIZE][MAX_NAME_LEN];
int i=0,j=0,k=0;

/*
Perfoming operations
*/

//Tree Structure
struct tree
{
char a[20];
int s;
struct tree *left,*right;
}*root=NULL,*tt[20]={NULL},*temp,*temp2,*t2,*ri,*le;


// Queur Structure
struct pqu
{
int info;
char a[20];
struct pqu *ptr;
}*front=NULL,*t,*par,*t1,*p1,*p2;

// Pointer to queue
struct pqu* fp(int info)
{
struct pqu *p=NULL;
for(t1=front;t1->info<info&&t1!=NULL;t1=t1->ptr)
{
p=t1;
}
return (p);

}

//PUSH operation
void enqu(char a[20],int p)
{
t=(struct pqu*)malloc(sizeof(struct pqu));
strcpy(t->a,a);
t->info=p;
t->ptr=NULL;
if(front==NULL)
{
front=t;
}
else
{
par=fp(p);
if(par==NULL)
{
t->ptr=front;
front=t;
}
else
{
t->ptr=par->ptr;
par->ptr=t;
}
}
}

// POP operation
struct pqu* dequ()
{
t1=front;
front=front->ptr;
return t1;
}

//Get Info of a leaf
void info(char c[2])
{
int m=0,i;
temp2=root;
while(strcmp(c,temp2->a)!=0)
{
t2=temp2->left;
for(i=0;i<strlen(t2->a);i++)
{
if(t2->a[i]==c[0])
{
temp2=temp2->left;
m=1;
cout<<”0&”;
break;
}
}
if(m!=1)
{
temp2=temp2->right;
cout<<&”1&”;
}
m=0;
}
}

// Insert Operation
void insert()
{
char a1[20],b1[20],v1[20];
int i,j,z=0,l;
while(front!=NULL)
{
p1=dequ();
strcpy(a1,p1->a);
l=p1->info;
p2=dequ();
if(p2==NULL)
break;
strcpy(b1,p2->a);
strcpy(v1,a1);
temp=(struct tree*)malloc(sizeof(struct tree));
strcpy(temp->a,strcat(v1,b1));
temp->s=l+p2->info;
temp->left=NULL;
temp->right=NULL;
temp2=temp;
root=temp;
for(i=0;i<z;)
{
if(strcmp(tt[i]->a,a1)==0)
{
temp->left=tt[i];
for(l=i;l<z;l++)
{
tt[l]=tt[l+1];
}
i=0;
continue;
}
else if(strcmp(tt[i]->a,b1)==0)
{
temp->right=tt[i];
for(l=i;l<z;l++)
{
tt[l]=tt[l+1];
}
i=0;
continue;
}
i++;
}
if(temp->left==NULL)
{
le=(struct tree*)malloc(sizeof(struct tree));
strcpy(le->a,a1);
le->left=NULL;
le->right=NULL;
temp2->left=le;
}
if(temp->right==NULL)
{
ri=(struct tree*)malloc(sizeof(struct tree));
strcpy(ri->a,b1);
ri->left=NULL;
ri->right=NULL;
temp2->right=ri;
}
if(front!=NULL)
enqu(temp2->a,temp2->s);
tt[z++]=temp2;
}
}

//Display Tree
void disp(struct tree *rt)
{
if(rt!=NULL)
{
disp(rt->left);
//cout<<””<<rt->a;disp(rt->right);
}
}

//Display Name
void displayname(int idx)
{
cout<<name[idx]<<”: ”;
}

/*
End of operations
*/


//Main Method

int main()
{
while(1)
{
//Clear Screen
clrscr();

cout<<”Enter the total no of leafs : ”;
cin>>n;


for(i=0;i<n;i++,alpha++)
{
cout<<”Enter Name: ”;
cin>>name[i];

//itoa(alpha,m,10);
//strcpy(m,(char*)alpha[i]);
*m=(char)alpha; //Assigning Local Values
strcpy(b[i],m);

cout<<”Enter frequency for ”<<name[i]<<” : ”;
cin>>g;

//Enque to the list
enqu(m,g);

} //Repeat till all the leafs data entered

insert(); // Form the tree
disp(root); // Display the codeword of leaf from root


for(i=0;i<n;i++)
{
displayname(i);
info(b[i]);
cout<<”n”;
}

cout<<”nDo You Want To Continue Y or N: ”;
cin>>re;
clrscr();
if(re=='y'||re=='Y')
continue;
else
break; //exit(0);
}

return 0;
}//end of while

Output:

I am also making this as an open source code. Anyone can edit, share, re-distribute the code.

For further queries, please mail to psrdotcom@gmail.com

Featured Post

Java Introdcution

Please send your review and feedback to psrdotcom@gmail.com