Skip to content

Instantly share code, notes, and snippets.

@mnyamor
mnyamor / process.js
Last active October 27, 2016 22:21
Node process
var questions = [
"What is your name?",
"what is your favourite hobby?",
"what is your favourite programming language?"
];
var answers = [];
function askQuestion(i) {
@mnyamor
mnyamor / timing.js
Created October 27, 2016 22:42
Global timing functions - setTimeout
/*
Working with Node.js asynchronously through the use of timing functions.
The timing functions are:- setTimeout, clearTimeout, setInterval, and clearInterval
- setTimeout will create a delay of a certain time and then invoke a callback function
*/
var waitTime = 3000;
console.log("wait for it");
setTimeout(function(){
@mnyamor
mnyamor / globalTiming.js
Last active October 27, 2016 23:16
Global timing functions - setInterval
/*
Working with Node.js asynchronously through the use of timing functions.
The timing functions are:- setTimeout, clearTimeout, setInterval, and clearInterval
- setTimeout will create a delay of a certain time and then invoke a callback function
- setInterval will not stop, its like a heartbeat
- clearInterval & clearTimeout will stop intervals or timeouts that are currently running.
*/
var waitTime = 3000; //waitTime is our total time
var currentTime = 0; //currentTime is the amount of time that we've waited
@mnyamor
mnyamor / promptPerson.js
Last active October 30, 2016 15:38
Collecting information with Readline
/*Readline is a module that allows us to ask questions to our Terminal user.
The standard input(stdin) and standard output(stdout) objects that allow
us to easily control prompting a user with questions and saving those answers.
*/
//create an instance of the readline object which will create the propmt.
var readline = require('readline');
var realPerson = {
name: '',
saying: []
@mnyamor
mnyamor / events.js
Last active October 30, 2016 16:01
Handling Events With EventEmitter
/*The Event Emitter is Node.js's implementation of the pub/sub design pattern.
It allows us to create listeners for an emit custom Events.
In fact, every time we've used that on function to listen for a new Event,
we've already been using an implementation of the EventEmitter.
*/
// create a variable for events and require the events module
var events = require('events');
// create a new instance of a variable called emitter by using the new keyword with the event module, .Event Emitter
// The EventEmitter is a constructor and we create a new instance of the EventEmitter.
@mnyamor
mnyamor / EventEmitter.js
Created October 30, 2016 16:18
Handling Events With EventEmitter 2
var EventEmitter = require('events').EventEmitter;
// We can use the utilities module to help us to have the Person object inherit the EventEmitter
var util = require('util');
/* -create a Person object
-create a constructor function here for this object
-in this constructor function, we're going to take in the Person's name
*/
var Person = function(name) {
// this Person's name will be set to the name value that is passed to this constructor function.
@mnyamor
mnyamor / customModules.js
Created October 30, 2016 16:31
Exporting Custom Modules
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var Person = function(name) {
this.name = name;
};
util.inherits(Person, EventEmitter);
/* module.exports = Person; -> object returned by the require export.
we could export this, move it to a separate file then require it as a separate module..
@mnyamor
mnyamor / exec.js
Created October 30, 2016 22:37
Creating Child Process With Exec
/*
- Node.js comes with a Child Process module which allows you to execute external processes in your environment.
- In other words, your Node.js app can run and communicate with other applications on the computer that it is hosting
- Two main functions used to create Child Processes: spawn and execute
-
*/
//The Child Process module contains the execute function
var exec = require("child_process").exec;
//exec('open http'://www.google.com');
@mnyamor
mnyamor / spawn.js
Created October 30, 2016 22:50
Creating Child Process With Spawn
/*alwaysTalking.js
var sayings = [
"You may delay, but time will not.",
"Tell me and I forget. Teach me and I remember. Involve me and I learn.",
"It takes many good deeds to build a good reputation, and only one bad one to lose it.",
"Early to bed and early to rise makes a man healthy, wealthy and wise.",
"By failing to prepare, you are preparing to fail.",
"An investment in knowledge pays the best interest.",
"Well done is better than well said."
@mnyamor
mnyamor / list.js
Created October 30, 2016 23:03
Listing Directory Files
/*
- When we use any methods of the fs module we are given the option to use them synchronously or asynchronously.
- reading files synchronously will block the single Node.js thread so all other connections will wait for this synchronous recal
*/
//include the file system module - we can do just about anything with files and directories
var fs = require('fs');
// create var for files ..
// var files = fs.readdirSync('./lib'); //READ FILES FROM LIBRARY FOLDER..