This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // this is example to make http calls in angular 4/5 | |
| // not silver bullet to follow but it get's job done | |
| import { HttpClient,HttpHeaders,HttpParams } from '@angular/common/http'; | |
| export class SomeHttpService { | |
| constructor(private httpClient: HttpClient){} | |
| // simple get method |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| async function loginController(){ | |
| try{ | |
| const a = await loginService(). | |
| catch((error)=> {throw new CustomErrorHandler({code: 101, message:"a failed",error: error})}); | |
| const b = await someUtil(). | |
| catch((error)=> {throw new CustomErrorHandler({code: 102, message:"b failed",error: error})}); | |
| //someoeeoe | |
| if(a && b) console.log("no one failed") | |
| }catch(error){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // let's code some emitters | |
| function Emitter(){ | |
| this.events = {}; | |
| } | |
| const emtr = new Emitter(); | |
| // on function to push events in array | |
| Emitter.prototype.on = function(type,listener){ | |
| this.events[type] = this.events[type] || []; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var express = require('express'); | |
| var app = express(); | |
| const expressjwt = require('express-jwt') | |
| const jsonwebtoken = require('jsonwebtoken'); | |
| // this will verify the token before going to actual requested route (middleware) | |
| // except the "/login" route every request needs token in header which will look like "Authorization:Bearer eyJh..........." refer image in story | |
| app.use(expressjwt({secret: "some key"}).unless({path: ['/', '/login']})) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| async function getData(){ | |
| const a = await someFunction().catch((error)=>console.log(error)); | |
| const b = await someOtherFunction().catch((error)=>console.log(error)); | |
| if(a && b ) console.log("some result") | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| async function check(req, res) { | |
| try { | |
| const a = await someOtherFunction(); | |
| const b = await somethingElseFunction(); | |
| res.send("result") | |
| } catch (error) { | |
| res.send(error.stack); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| async function check(req, res) { | |
| someOtherFunction().then((a) => { | |
| somethingElseFunction().then((b) => { | |
| res.status(200).json({a: a, b: b}); | |
| }).catch((error) => { | |
| res.send("error"); | |
| }) | |
| }).catch((error) => { | |
| res.send("error"); | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // traditional way of writing code | |
| const router = require('express').Router(); | |
| router.get('/check', check); | |
| module.exports = router; | |
| async function check(req, res) { | |
| someOtherFunction().then((a) => { | |
| somethingElseFunction().then((b) => { | |
| res.status(200).json({a: a, b: b}); | |
| }).catch((error) => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * this class helps us to make http calls | |
| */ | |
| public class HttpCaller { | |
| private Context context; | |
| //private ProgressDialog pDialog; | |
| private HttpProgressDialog httpProgressDialog; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // source : https://www.npmjs.com/package/jsonwebtoken | |
| // check token information at : https://jwt.io/ | |
| const jwt = require('jsonwebtoken'); | |
| // secret key | |
| const secretKey = 'some secret key'; | |
| // this is for verifying and creating token middleware | |
| app.use((req, res, next) => { | |
| // if the requested URL is login then create token otherwise verify |