Skip to content

Instantly share code, notes, and snippets.

View saiumesh535's full-sized avatar
🤒
so long

sai umesh saiumesh535

🤒
so long
View GitHub Profile
@saiumesh535
saiumesh535 / app.js
Last active June 27, 2017 10:57
MySQL connector or singleton for express node app
// getting connection from mysql.js file
// creating new mysql connection and checking it
const mysqlConnection = require('./model/mysql');
const mysql = require('mysql');
app.get('/testmysql', function(req, res) {
console.log(mysqlConnection.name);
mysqlConnection.createConnection().then((connection) => {
let query = "select * from zeus_user";
let table = [];
query = mysql.format(query, table);
@saiumesh535
saiumesh535 / AppleOrOrange.py
Last active July 17, 2017 06:13
Sklearn Python Example
# source https://www.youtube.com/watch?v=cKxRvEZd3Mw
from sklearn import tree;
# 0 for bumpy
# 1 for smooth
features = [[140,1], [130,1], [150,0], [170,0]]
#0 for apple
#1 for orange
labels = [0 ,0, 1, 1];
@saiumesh535
saiumesh535 / newPromise.js
Last active August 21, 2017 18:18
Async Await is new form writing in JavaScript/Node 8
// for examples goto https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
// this function will return a promise
function multiplication(a, b) {
return new Promise((resolve, reject) => {
resolve(a * b)
})
}
// whn you write async before function it allows you to return promise which was being returned from other function
@saiumesh535
saiumesh535 / jsonwebtokenexample.js
Last active August 11, 2017 06:12
creating and verifying "jsonwebtoken" in express as middleware.
// 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
@saiumesh535
saiumesh535 / HttpCaller.java
Created August 28, 2017 11:43
Volley Server request class
/**
* this class helps us to make http calls
*/
public class HttpCaller {
private Context context;
//private ProgressDialog pDialog;
private HttpProgressDialog httpProgressDialog;
@saiumesh535
saiumesh535 / somefile.js
Last active February 8, 2018 23:01
Async await without try catch
// 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) => {
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");
})
@saiumesh535
saiumesh535 / asyncawait.js
Last active November 3, 2017 06:38
Asyncawait
async function check(req, res) {
try {
const a = await someOtherFunction();
const b = await somethingElseFunction();
res.send("result")
} catch (error) {
res.send(error.stack);
}
}
@saiumesh535
saiumesh535 / asyncawaitwithouttrycatch.js
Last active February 19, 2020 18:23
async await without try catch
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")
}
@saiumesh535
saiumesh535 / app.js
Last active November 8, 2017 11:09
Creating JWT
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']}))