Showing posts with label Articles. Show all posts
Showing posts with label Articles. Show all posts

Thursday, December 12, 2019

Send Emails without a server-side code with Angular



Without any server-side code hosted on a server, you can send emails through your Angular application with smtpJS, a client-side Javascript library.

First you need to download this library and include it into the necessary component of the Angular Application.
Click here to download the library file.

Next , put this downloaded js file into the assets folder found in src directory of the project.

Next we have to get the help of an email service provider to send the email. I here use Elastic Email site for this. First go to the Elastic Email site to create an account.  Just complete the sign up process, it's that simple.
After logging into the site, go to Start ---->  Connect To SMTP API . 


Next, you can see a button to create new credentials. Create a new credentials following the simple steps. The system will generate a password for you. Copy that password and save it somewhere before you close that popup model.

Finally you'll see something like this.


Here, your user name and password (You saved it earlier) can be seen .These details are used to access this.

Coding part

Go to your angular project's relevant component from which an email must be sent.

1. Import the smtp.js file from assets folder to the component by  typing
    import './../../../assets/smtp.js'; <!-- path might change according to your file structure-->
declare let Email : any;

2. Write the code to send the email with the user-inputs within onSubmit() method as below,
Here , within Email.send(....);  method, you can give the elastic mail registered email as the username , and earlier saved password for the password field.  From: email should be equal to the elastic mail's registered email and you can set the receiver's email under To field.
Other fields can be taken from your form created .

That's it.

Sunday, November 3, 2019

Simply Deploy your Angular App on Netlify



This article is all about how you can deploy your Angular application securely on Netlify server . This is a free place for you to deploy your application.

First you need to have a Netlify Accout (Create if not)  and a GitHub repo of your project.
( I assume that you already have a github repo of your Angular project.)
I teach you the way with a sample project I created just for fun.

Let's begin

1. First go to your Netlify account. There you can see a button     "New site from Git"(Fig 1.1). Click on it.
Fig 1.1

2. Then select the repository site. I here select GitHub.(You can select the site where your project is.)
When selected, you are redirected to authorize the access from netlify and there you can select the repository that you need to deploy.

 3. Select the repository again and go to step 3. (Fig 1.2)
Fig 1.2


Here in this page, you have to fill some information. (you can change these later too)
     You can specify which branch needs to be deployed.
     For Build Command , give ng build --prod
     For Publish directory , give dist/<your project name>
                     (You can find your correct project name at the end of angular.json file of your project. Ex: dist/my-blogger-web  as in Fig(1.3))
Fig 1.3

If all set. Click the button "Deploy site". You will be directed to something like this in Fig 1.4.
Fig 1.4


4. Next, go to Settings tab and copy the API ID  and save it somewhere because we are gonna need it later. (Fig 1.5)
[Here, you can change your site name also.]
Fig 1.5


5. We need to find a Personal Access Token key . 
              Click on Avatar picture of your profile on right top corner,                 select User settings,
               Select Applications from the menu,
               Under Personal access tokens, click New access token button .
(Fig 1.6, Fig 1.7)
Fig 1.6

Fig 1.7


     Give a simple description for the token so that you can identify why this token is used, when you check tokens sometimes later. 
    Click the button to Generate a token. Copy and paste that token somewhere to use it later. Then click Done! at last.

***Here after , you have some configuration inside your angular project.***

6.  Open the command window inside your Angular project's root folder.

  and run this command ng add @netlify-builder/deploy
  *There you are asked to enter API ID: you can enter it from where you copied and saved it earlier.
  ** You are asked  next to enter personal Acccess token which you generated earlier . Please, do not enter it here.Just press enter and skip it.( Because these details are entered in your angular.json file which can be tracked in your github repo easily so that outsider can use your API ID and access token to get access to your site. see Fig 1.8 )


Fig 1.8



7. Next you have to set up the access token key as an environment variable of your computer.
    Right click on This PC icon on desktop of you computer ==>     Properties ==> Advanced system settings ==> Advanced tab ==>Environment Variables ==> User Variables

  Under User variables , you have to enter a new variable, Variable Name as NETLIFY_TOKEN and  Variable value as the access token key generated earlier and saved for later use by you. see Fig 1.9
Fig 1.9


9. Next you have to run 
               ng run <your project name>:deploy
     Plz, give your project name when running the above command. Ex: ng run my-blogger-web:deploy

[For Angular vCLI 8.3.0 and above, just run ng deploy  ]

  This creates a folder dist/<Your project name>  which you gave as the Publish directory for netlify site.

10. Next part is, open your .gitignore file of your project and delete the line with /dist to track dist folder to be pushed to gitHub.

 At last commit and push.

Here you go, go to your netlify site, and Click Trigger Deploy  button to deploy. You can change 'deploy settings' there if you want.





All done!

Your site is available at https://<your site name>.netlify.com

                             ex: https://udithdemo.netlify.com


Thank you.


Monday, October 21, 2019

All about .replaceAll() in Java

 


  java.lang.String class  brings you a very powerful method  to do replacements within a string.
   This method allows you to target  specific substrings using regular expression ,so that you can even remove and replace any pattern within a string.
 
Mainly, this method can

  •    replace a given substring found any where in the string
  •    replace a given character found any where in the string
  •    replace a given reg expression found any where in the string    
    Even though there are separate methods like replace(char old, char new), replace(charSequence old, charSequence new) to do above functions, this replaceAll() method acts as a versatile method for all those functions.   

This is how this method is defined in String class,
 
            String replaceAll(String regex , String new)  

   As you can see , this method returns a new string.  Actually, a string is immutable in JAVA , so it is not possible to alter a string. This method returns a new string instead.You need to catch it with a string variable.

      *This method accepts to two parameters which must be in type String. So , when you need to replace a character, you have to use a double quotation for the characters (instead of single quotations which are used for representing char). 

Examples: 

    
Character replacement

String replacement
Regex replacement


Keep in Touch!!!

Friday, September 20, 2019

How to use ng-bootstrapped Popup Modals within Angular Components


        Hello guys, this article is about how to add ng-bootstrapped modals within your components in Angular application. Going through this article, a very beginner can learn how to add and use different bootstrapped gadgets and other stuffs  with your Angular application.
       I have already created a very simple project but not yet bootstrapped. I just need to add a popup message box when clicking on the button on the page. 
Below is how my application looks right now and I have used only app.component.ts and app.component.html files for this.

Initial Web Page
app.component.html
app.component.ts
     Now let's begin to add the requirements to continue.

Step 1

  • Go to the root project folder of your project.
  • Install ng-bootstrap to your project by running 
  •                                     npm install --save @ng-bootstrap/ng-bootstrap
  • Go to app.module.ts file and import the Ngbmodule inside import array. 
                        import {NgbModule} from '@ng-bootstrap/ng-bootstrap';           
app.module.ts



  • Go to index.html and copy the bootstrap CDN code snippent from bootstrap site , within the scope of <head></head> tags.
  •                             ( For now,    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> )

Step 2

  • Go to ng-bootstrap site and find the bootstrap code section for Modal.(Modal-ng-bootstrap) 
   
Section for Modals
     
  • Within the section I have circled in , when you click the button with name "</>Code", you can find a code file with the name modal-component.ts with the following code snippet.


  • Here , in the above code, you can find two classes as NgbdModalContent (squared in red colour) and NgbdModalComponent (squared in purple colour). 
  • Now it's time to copy code from this snippet and paste it into your app.component.ts file. 
  • First, import the modules shown in Section 1 in the above image into your app.component.ts file.
  • Secondly, Copy the whole code in section two and paste it at the very end of   the app.component.ts file.
  • Thirdly, inject the service into the constructor of your AppComponent class in app.component.ts file ,as shown in section 3 .
  • And, Lastly, Copy the method "copy()" shown in the section 4 as a netive method of  the AppComponent class.
  • This method can be used in an (click) event within the button in html file now.


Step 3


  • Enter NgbdModalContent class  in declarations array as well as an entryComponent in app.module.ts file.       

        
app.module.ts

Finally

      This is my app.component.ts file and html file .
app.component.ts

app.component.html

Final View when clicked on the button on the page,




Enjoy bootstrap!!!




Friday, August 30, 2019

Best Learning Resources for Developers

A collection of online learning resources
1. HTML/CSS
            b. https://css-tricks.com/
2. JavaScript
          c. https://es6.io/
3. PHP  
4. IOT / Electronics
          a. https://www.adafruit.com
          b. https://www.sparkfun.com/
5. Machine learning

   6. Other common resources

                            ~ End ~

Wednesday, July 17, 2019

Upload Images and save them in a database( Angular 8 + Spring boot + Mysql )


               This article is all about uploading an image through your angular project , sending it into the backend and saving it in a database.
 Not only that, the way how to query the database and viewing back the image in the frontend  is also explained here. 
[As this article is written for explaining the simple mechanism of uploading an image,  web page decorations are not done, only the relevant parts are designed for explanation.]

  •    The frontend  used is Angular 8.
  •    The backend use is  Spring boot
  •    The database used is Mysql


 Please note that,

  • Some basic knowledge about angular ,spring boot and mysql is essential.( because the way of project creating in those frameworks is not discussed as it makes this article lengthier.)
  • As IDEs , VS code is used for Angular and IntelliJ IDE  is used for  Spring boot.
 

                                          

1.0 Frontend 
      First , create an angular project . ( See how )
      We need HttpClientModule for dealing with the backend.So, We 
      have to import that module to the project.
       Go to src/app/app.module.ts

  • Add the following code lines in app.module.ts file as in the image

       below.
         import { HttpClientModule } from '@angular/common/http';

     and, declare the module in imports array.














         
                   






  • Go to app.component.html file and add the following code.


<h3>Image Uploading Module</h3>


<div>
<input type="file" (change)="onFileChanged($event)">

<img [src]="imgURL" height="200" *ngIf="imgURL">
</div>

<!-- upload image -->
<input type="button" (click)="onUpload()" value="upload">

<hr />

Received Image:

{{ receivedImageData['pic'] | json}}
<img [src]="convertedImage" >
    
The view will be like this.
In this code, 
(change)="onFileChanged($event)
 is used to listen to any file choosing event and trigger the onFileChange(event) method in .ts file.

imgURL, convertedImage are properties in the .ts file.












  • Go to src/app/app.component.html file and write the following code snippets in it.



Import HttpClient class from '@angular/common/http' .
And, inject it via the constructor of the class as in the image.
Write and complete your app.component.ts file as shown in the images.

As the backend is running on local port 8080, posting url is also given as preferred.
We send the image to the backend as a FormData,so that  the backend can retrieve it as a multipart file.
By,subscribing to the post method ,we have retrieved the returned image data by the backend and assigned them to the properties which are used to render in the view.














***You may find the code snippets here.






2.0 Spring Boot Backend

                Now,let's move to the backend part. 

1. Give a group name to the project
2. Give a artifact nae to the project
3.Add three dependencies given[Spring Web Starter, Spring Data JPA, MySQL Driver, Lombok] 
3. Genereate the project and download the zipped file of the project.  


  • Extract the zipped file .
  • Open your IDE(Here,IntelliJ IDE is used).
  • Open the extracted project by selecting the pom.xml file, through the IDE.


This is the file structure in the project I created. 




  • A controller class as TestController.java [Get code]
  • A model class as ImageModel.java [Get Code]
  • A repository interface as ImageRepository.java [Get code]











**ImageModel.java

**ImageRepository.java

**TestController.java

Here , in the controller file, image is captured in MulipartFile format . From the frontend, the image data is sent as a form data by appending the data to the request parameter named "myFile" .

*You can read about MultipartFile interface here.


*You need to edit the application.properties file , to connect to the mysql database according to your PC settings. (Read how)



Enjoy!

Send Emails without a server-side code with Angular

Without any server-side code hosted on a server, you can send emails through your Angular application with smtpJS, a client-side Javasc...