Skip to content

Instantly share code, notes, and snippets.

View mohankumar-i2i's full-sized avatar

Mohan Kumar mohankumar-i2i

  • ideas2it technologies
  • chennai,tamil nadu,India
View GitHub Profile
@mohankumar-i2i
mohankumar-i2i / ec2-user-data.sh
Last active August 6, 2020 02:05
Sample user data for ec2
#!/bin/bash
# install httpd (Linux 2 version)
# create role with s3 write access privilege and set to ec2 instance
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
usermod -a -G apache ec2-user
chown -R ec2-user:apache /var/www/html
chmod 2775 /var/www/html
const cluster = require("cluster");
const express = require("express");
const numCPUs = require("os").cpus().length;
const app = require
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
// Fork workers.
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
@mohankumar-i2i
mohankumar-i2i / sync-async-hybrid.js
Created February 14, 2020 09:38
hybrid function
var fs = require('fs');
const cache = {};
function readFile(fileName, callback) {
if (cache[fileName]) {
return callback(null, cache[fileName])
}
fs.readFile(fileName, (err, fileContent) => {
if (err) {
console.log(err);
const https = require('https');
const crypto = require('crypto');
const fs = require('fs');
const start = Date.now();
setImmediate(() => console.log('this is set immediate 1'));
setImmediate(() => console.log('this is set immediate 2'));
setImmediate(() => console.log('this is set immediate 3'));
const crypto = require("crypto");
const start = Date.now();
function logHashTime(label) {
crypto.pbkdf2("a", "b", 100000, 512, "sha512", () => {
console.log("Hash-"+label+": ", Date.now() - start);
});
}
logHashTime(1);
logHashTime(2);
@mohankumar-i2i
mohankumar-i2i / better-nodejs-require-paths.md
Created January 22, 2019 17:16 — forked from branneman/better-nodejs-require-paths.md
Better local require() paths for Node.js

Better local require() paths for Node.js

Problem

When the directory structure of your Node.js application (not library!) has some depth, you end up with a lot of annoying relative paths in your require calls like:

const Article = require('../../../../app/models/article');

Those suck for maintenance and they're ugly.

Possible solutions

@mohankumar-i2i
mohankumar-i2i / Mongo.md
Created January 2, 2019 05:56 — forked from jnewman12/Mongo.md
Data Modeling With Mongo

Data Modeling with MongoDB

mongo


Objectives

  • Understand model relationships in MongoDB
  • Understand One-to-Many relationships
@mohankumar-i2i
mohankumar-i2i / install-docker.sh
Created September 24, 2018 08:44 — forked from sheikhwaqas/install-docker.sh
Install Docker & Docker-Compose on Ubuntu 16.04 LTS (Xenial). Run this file with sudo.
# Install Pre-requisites
apt update
apt install apt-transport-https ca-certificates curl software-properties-common -y
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
apt update
# Install Docker CE
apt install docker-ce -y
@mohankumar-i2i
mohankumar-i2i / node-and-express.md
Created September 22, 2018 18:13 — forked from anotheruiguy/node-and-express.md
Set up a new Node.js project with Express >4.0: a newb's guide

And my article is deprecated!

As of just writing this, Express 4.0 was released and there are points in there that no longer matter. So, this remains as a great >4.0 article.

Node.js is the red-hot new hotness! You can't throw a stick on the internet without hitting someone talking about Node. But why? For one, it's built on JavaScript which is completely ubiquitous. So, why not build a development stack and server on JavaScript? I would argue that the installation is almost painless while the terseness of the language is not.

While you can create apps 100% from Node.js, the Express framework is a great tool that helps you solve many standard problems without having to write boilerplate code.

Node.js is here and it's not going anywhere anytime soon. So if you are new to Node.js, Express, and even JavaScript in general, this is a great newb's step-by-step guide to get started.

@mohankumar-i2i
mohankumar-i2i / async-foreach.js
Created September 19, 2018 10:31 — forked from atinux/async-foreach.js
JavaScript: async/await with forEach()
const waitFor = (ms) => new Promise(r => setTimeout(r, ms))
const asyncForEach = (array, callback) => {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array)
}
}
const start = async () => {
await asyncForEach([1, 2, 3], async (num) => {
await waitFor(50)