Skip to content

Instantly share code, notes, and snippets.

@tankala
tankala / index.js
Created June 9, 2018 11:28
Code for building static content server using Node.js
var express = require('express');
var app = express();
//Serves resources from public folder
app.use(express.static('public'));
app.listen(3000);
@tankala
tankala / ParallelizingTasksInNode.js
Created July 1, 2018 10:01
Downloading 2 avatars in parallel at a time using Async in Node.js
const request = require('request');
const fs = require('fs');
const async = require('async');
const avatarsNamesArray = ['1.png','2.png','3.png','4.png','5.png','6.png','7.png','8.png','9.png','10.png'];
getAndStoreAvatar = function(avatarName, callback) {
console.log(avatarName + ' is downloading');
let stream = request
.get('https://api.adorable.io/avatars/285/' + avatarName)
@tankala
tankala / ParallelizingTasksInJava.java
Last active July 1, 2018 10:49
Downloading 2 avatars in parallel at a time in Java
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
@tankala
tankala / AppJsWithTimeMeasurement.js
Created July 21, 2018 13:32
Review your Express.js application performance by yourself
// Section 1
var express = require('express');
const bodyParser = require('body-parser');
const { EventEmitter } = require('events');
const morgan = require('morgan');
const app = express();
const profilingEventEmitter = new EventEmitter();
// Section 2
profilingEventEmitter.on('middleware', ({ req, name, elapsedMS }) => {
@tankala
tankala / app.js
Created July 28, 2018 12:04
Profiling Node.js application using v8-profiler
// Imports
var express = require('express');
const bodyParser = require('body-parser');
const morgan = require('morgan');
const app = express();
const controller = require('./controller');
const profiler = require('v8-profiler');
const fs = require('fs');
// Middlewares
@tankala
tankala / controller.js
Created July 28, 2018 12:07
Controller
const wordWizard = require('./wordWizard')
exports.getWordsCountInDesc = function (req, res) {
let callBack = function (err, response) {
if (err) {
res.json({});
} else {
res.json(response);
}
}
@tankala
tankala / wordWizard.js
Created July 28, 2018 12:10
Word Wizard for calculating words count with business logic flaw
const stopWords = ["a", "about", "above", "after", "again", "against", "all", "am", "an", "and", "any", "are", "as", "at", "be", "because", "been", "before", "being", "below", "between", "both", "but", "by", "could", "did", "do", "does", "doing", "down", "during", "each", "few", "for", "from", "further", "had", "has", "have", "having", "he", "he'd", "he'll", "he's", "her", "here", "here's", "hers", "herself", "him", "himself", "his", "how", "how's", "i", "i'd", "i'll", "i'm", "i've", "if", "in", "into", "is", "it", "it's", "its", "itself", "let's", "me", "more", "most", "my", "myself", "nor", "of", "on", "once", "only", "or", "other", "ought", "our", "ours", "ourselves", "out", "over", "own", "same", "she", "she'd", "she'll", "she's", "should", "so", "some", "such", "than", "that", "that's", "the", "their", "theirs", "them", "themselves", "then", "there", "there's", "these", "they", "they'd", "they'll", "they're", "they've", "this", "those", "through", "to", "too", "under", "until", "up", "very", "was", "we",
@tankala
tankala / cpuProfiling.js
Created July 28, 2018 12:30
CPU profiling using v8-profiler
app.get('/doCPUProfiling/profileId/:profileId/durationInSec/:durationInSec', function (req, res) {
let profileId = req.params['profileId'];
let durationInMilliSec = req.params['durationInSec'] * 1000;
// Start profiling
profiler.startProfiling(profileId);
setTimeout(function () {
stopProfiling(profileId);
}, durationInMilliSec);
res.json({});
});
@tankala
tankala / correctWordWizard.js
Created July 28, 2018 13:18
Word Wizard code for calculating words count and sorting in Desc
const stopWordsSet = new Set(["a", "about", "above", "after", "again", "against", "all", "am", "an", "and", "any", "are", "as", "at", "be", "because", "been", "before", "being", "below", "between", "both", "but", "by", "could", "did", "do", "does", "doing", "down", "during", "each", "few", "for", "from", "further", "had", "has", "have", "having", "he", "he'd", "he'll", "he's", "her", "here", "here's", "hers", "herself", "him", "himself", "his", "how", "how's", "i", "i'd", "i'll", "i'm", "i've", "if", "in", "into", "is", "it", "it's", "its", "itself", "let's", "me", "more", "most", "my", "myself", "nor", "of", "on", "once", "only", "or", "other", "ought", "our", "ours", "ourselves", "out", "over", "own", "same", "she", "she'd", "she'll", "she's", "should", "so", "some", "such", "than", "that", "that's", "the", "their", "theirs", "them", "themselves", "then", "there", "there's", "these", "they", "they'd", "they'll", "they're", "they've", "this", "those", "through", "to", "too", "under", "until", "up", "very", "
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "root",
database: "Employee"
});
con.connect(function (err) {