Skip to content

Instantly share code, notes, and snippets.

@salmanx
salmanx / child_process_exec.js
Created October 15, 2018 14:12
explanation how child process works in node js
const exec = require('child_process').exec;
exec("open https://facebook.com");
exec('ls', function(err, stdout){
if(err)
console.log(err)
console.log("Listing finished..")
console.log(stdout);
@salmanx
salmanx / event.js
Created October 15, 2018 11:19
simple code example how EventEmitter() works in node js
// Basic events functionality
// const EventEmitter = require('events');
// const emitter = new EventEmitter();
// emitter.on('messageLogged', function(args){
// console.log(`${args.name} : ${args.age}`)
// console.log("Listener called");
// });
@salmanx
salmanx / readline.js
Created October 15, 2018 10:16
code example for readline module in node js
const readline = require('readline');
const rl = readline.createInterface(process.stdin, process.stdout);
const realPerson = {
name: "",
sayings: []
};
rl.question("Who is real person? ", function(answer){
realPerson.name = answer;
rl.setPrompt(`What would ${realPerson.name} say? `);
@salmanx
salmanx / timers.js
Created October 15, 2018 07:48
code example of setTimeout() and setInterval() for node js
var readline = require('readline');
var waitTime = 3000;
var currentTime = 0;
var waitInterval = 500;
var percentageWaited = 0;
function writeWaitingPercentage(p){
readline.clearLine(process.stdout);
readline.cursorTo(process.stdout, 0)
@salmanx
salmanx / process.stdout.js
Created October 15, 2018 07:14
snippet for standard input output example for node js
const questions = [
"What is your name?",
"What is fav hobby?",
"What is your fav programming language?"
];
const answers = [];
function ask(i){
process.stdout.write(`\n\n ${questions[i]} \n\n`);
const mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/playground", { useNewUrlParser: true })
.then(() => console.log("Mongo db is connected!"))
.catch((err) => console.error("Could not connect to db ...", err));
const courseSchema = new mongoose.Schema({
name: { type: String, required: true },
author: { type: String, required: true, minlength: 3, maxlength: 255 },
category: {
@salmanx
salmanx / excerpt.pipe.ts
Created November 23, 2017 07:42
Custom pipe for showing summary text in angular
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: "excerpt"
})
export class ExcerptPipe implements PipeTransform{
transform(text: string, limit?: number){
@salmanx
salmanx / seed_db.js
Created November 22, 2017 07:24
seeding data in mongo db client
require('dotenv').config();
const MongoClient = require('mongodb').MongoClient;
const bcrypt = require('bcrypt');
const users = require('./users');
function seedCollection(collectionName, initialRecords){
MongoClient.connect(process.env.DB_CONN, (err, db) => {
console.log("connected to mongo db");
@salmanx
salmanx / auto-grow.directive.ts
Last active November 23, 2017 07:37
A simple angular directive to expand input field
import { Directive, ElementRef, Renderer } from '@angular/core';
@Directive({
selector: '[autoGrow]',
host: {
'(focus)' : 'onFocus()',
'(blur)' : 'onBlur()'
}
})
export class AutoGrowDirective {
@salmanx
salmanx / dropdown.directive.ts
Last active January 2, 2018 06:03
simple angular directive for dropdown menu in navbar
import { Directive, HostBinding, HostListener } from '@angular/core';
@Directive({
selector: '[rbDropdown]'
})
export class DropdownDirective {
private isOpen = false;
@HostBinding('class.open') get opened(){
return this.isOpen;