This repository has been archived by the owner on Oct 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
darthcav
committed
Dec 8, 2016
1 parent
903d2fd
commit 6b8836b
Showing
59 changed files
with
918 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
"use strict"; | ||
|
||
const express = require("express"); | ||
const app = express(); | ||
const port = 8209; | ||
|
||
app.get("/", function (req, res) { | ||
res.send("Hello World!"); | ||
}); | ||
|
||
app.listen(port, function () { | ||
console.log(`Example app listening on port ${port}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
"use strict"; | ||
console.log("Hello World!"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
"use strict"; | ||
|
||
const http = require("http"); | ||
const hostname = "127.0.0.1"; | ||
const port = 4003; | ||
|
||
const server = http.createServer((req, res) => { | ||
res.statusCode = 200; | ||
res.setHeader("Content-Type", "text/plain"); | ||
res.end("Hello World!"); | ||
}); | ||
|
||
server.listen(port, hostname, () => { | ||
console.log(`Server running at http://${hostname}:${port}/`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
"use strict"; | ||
|
||
let times = 0; | ||
let a = setInterval(()=>{ | ||
const max_times = 10; | ||
if(++times > max_times) | ||
{ | ||
clearInterval(a); | ||
} | ||
else | ||
{ | ||
console.log(`Callback called ${times} times.`); | ||
} | ||
}, 500); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
"use strict"; | ||
|
||
const label = "10-times" | ||
let times = 0; | ||
console.time(label); | ||
let a = setInterval(()=>{ | ||
const max_times = 10; | ||
if(++times > max_times) | ||
{ | ||
clearInterval(a); | ||
console.timeEnd(label); | ||
} | ||
else | ||
{ | ||
console.log(`Callback called ${times} times.`); | ||
} | ||
}, 500); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
"use strict"; | ||
console.log(`Am I the main program? ${require.main === module}`); | ||
console.log(`My name is ${require.main.filename}`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
"use strict"; | ||
|
||
const mocha = require("mocha"); | ||
|
||
console.log(module.filename); | ||
console.log(module.id); | ||
console.log(module.loaded); | ||
console.log("Module children:"); | ||
module.children.forEach((child) => { | ||
console.log(child.id); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
"use strict"; | ||
|
||
const os = require("os"); | ||
|
||
let GIGABYTE = 9.31322575 * 1e-10; | ||
let type = os.type(); | ||
let platform = os.platform(); | ||
let release = os.release(); | ||
let arch = os.arch(); | ||
let hostname = os.hostname(); | ||
let homedir = os.homedir(); | ||
let memory = os.totalmem() * GIGABYTE; | ||
let fmemory = os.freemem() * GIGABYTE; | ||
let networkInterfaces = os.networkInterfaces(); | ||
let cpus = os.cpus(); | ||
|
||
console.log("Type: " + type); | ||
console.log("Platform: " + platform); | ||
console.log("Release: " + release); | ||
console.log("Architecture: " + arch); | ||
console.log("Hostname: " + hostname); | ||
console.log("Homedir: " + homedir); | ||
console.log("Memory: " + memory + " GB"); | ||
console.log("Free memory: " + fmemory + " GB"); | ||
console.log("Network: " + JSON.stringify(networkInterfaces, null, 4)); | ||
console.log("CPUs: " + JSON.stringify(cpus, null, 4)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
"use strict"; | ||
|
||
const MyEmitter = require("./my-emitter"); | ||
const myEmitter = new MyEmitter(); | ||
|
||
myEmitter.on("event", function (s, i) | ||
{ | ||
if(i < 2) | ||
{ | ||
console.log(this); | ||
} | ||
console.log(s); | ||
}); | ||
myEmitter.once("one", function () | ||
{ | ||
console.log("single event occurred!"); | ||
}); | ||
for(let i = 1; i < 3; i++) | ||
{ | ||
myEmitter.emit("event", `(${i}) an event occurred!`, i); | ||
myEmitter.emit("one"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
"use strict"; | ||
|
||
const EventEmitter = require('events'); | ||
|
||
module.exports = class MyEmitter extends EventEmitter {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
"use strict"; | ||
|
||
const MyEmitter = require("./my-emitter"); | ||
const myEmitter = new MyEmitter(); | ||
|
||
myEmitter.on("error", (err) => | ||
{ | ||
console.error(err.message); | ||
}); | ||
|
||
myEmitter.emit("error", new Error("I am an error!")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
"use strict"; | ||
|
||
const EventEmitter = require('events'); | ||
|
||
module.exports = class MyEmitter extends EventEmitter {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
"use strict"; | ||
|
||
const MyEmitter = require("./my-emitter"); | ||
const myEmitter = new MyEmitter(); | ||
|
||
for(let i = 1; i < 3; i++) | ||
{ | ||
myEmitter.emit("event", `(${i}) an event occurred!`, i); | ||
myEmitter.emit("one"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
"use strict"; | ||
|
||
const EventEmitter = require('events'); | ||
|
||
module.exports = class MyEmitter extends EventEmitter | ||
{ | ||
constructor() | ||
{ | ||
super(); | ||
this.on("event", function (s, i) | ||
{ | ||
if(i < 2) | ||
{ | ||
console.log(this); | ||
} | ||
console.log(s); | ||
}); | ||
this.once("one", function () | ||
{ | ||
console.log("single event occurred!"); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
"use strict"; | ||
try | ||
{ | ||
const m = 1; | ||
const n = m + z; | ||
} | ||
catch (err) | ||
{ | ||
console.error(err instanceof ReferenceError); | ||
console.error(err.name); | ||
console.error(err.message); | ||
console.error(err.stack); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
"use strict"; | ||
const fs = require("fs"); | ||
fs.readFile("a.xyz", (err, data) => | ||
{ | ||
if (err) | ||
{ | ||
console.error(err.message); | ||
} | ||
else | ||
{ | ||
console.log("File 'a.xyz' exists!"); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
"use strict"; | ||
const net = require("net"); | ||
const connection = net.connect("localhost"); | ||
|
||
connection.on("error", (err) => | ||
{ | ||
console.error(err.message); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
"use strict"; | ||
module.exports = class CustomError extends Error | ||
{ | ||
constructor(msg) | ||
{ | ||
super(); | ||
this.name = this.constructor.name; | ||
this.message = msg || CustomError.defaultMessage; | ||
} | ||
|
||
static get defaultMessage() | ||
{ | ||
return "Customized Error"; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
"use strict"; | ||
const CustomError = require("./custom-error"); | ||
try | ||
{ | ||
throw new CustomError(); | ||
} | ||
catch (err) | ||
{ | ||
console.error(err instanceof CustomError); | ||
console.error(err.name); | ||
console.error(err.message); | ||
console.error(err.stack); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
"use strict"; | ||
|
||
start(); | ||
console.log(`##### Process title: ${process.title}`); | ||
console.log(`##### Process version: ${process.version}`); | ||
console.log(`##### Process ID: ${process.pid}`); | ||
console.log(`##### Process arguments: ${process.argv}`); | ||
console.log(`##### Process execution path: ${process.execPath}`); | ||
console.log(`##### Process execution arguments: ${process.execArgv}`); | ||
console.log(`##### Process version: ${process.version}`); | ||
console.log(`##### Process architecture: ${process.arch}`); | ||
console.log(`##### Process platform: ${process.platform}`); | ||
console.log(`##### Process current directory: ${process.cwd()}`); | ||
console.log("##### Process versions:\n" + JSON.stringify(process.versions, null, 4)); | ||
console.log("##### Process configuration:\n" + JSON.stringify(process.config, null, 4)); | ||
|
||
process.on("exit", (code) => | ||
{ | ||
console.log("##### Process memory usage: " + JSON.stringify(process.memoryUsage(), null, 0)); | ||
console.log(`##### Process uptime: ${process.uptime()}`); | ||
console.log(`##### Finishing the process with code ${code} #####`); | ||
}); | ||
|
||
process.on("uncaughtException", (err) => | ||
{ | ||
console.log(`##### Exception caught: ${err.message}`); | ||
}); | ||
|
||
function start() | ||
{ | ||
console.log("##### Starting the process #####"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
"use strict"; | ||
|
||
console.log("##### Process environment variables before change: " + | ||
JSON.stringify(process.env, null, 4)); | ||
process.env["HISTSIZE"] = 10; | ||
console.log("##### Process environment variables after change: " + | ||
JSON.stringify(process.env, null, 4)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
"use strict"; | ||
|
||
process.on("exit", (code) => | ||
{ | ||
console.log("##### Process memory usage: " + JSON.stringify(process.memoryUsage(), null, 0)); | ||
console.log(`##### Process uptime: ${process.uptime()}`); | ||
console.log(`##### Finishing the process with code ${code} #####`); | ||
}); | ||
|
||
process.on("uncaughtException", (err) => | ||
{ | ||
console.log(`##### Exception caught: ${err.message}`); | ||
process.exit(1); | ||
}); | ||
|
||
start(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
"use strict"; | ||
|
||
const buf = Buffer.from("¡Hola! ¿Cómo estáis?", "utf8"); | ||
let i = 0; | ||
for (let b of buf) | ||
{ | ||
console.log(`element: ${i++} - decimal: ${b} - hex: ${b.toString(16)}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
"use strict"; | ||
|
||
const buf = Buffer.alloc(1, "¡", "utf8"); | ||
let i = 0; | ||
for (let b of buf) | ||
{ | ||
console.log(`element: ${i++} - decimal: ${b} - hex: ${b.toString(16)}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
"use strict"; | ||
|
||
const str = "¡Hola! ¿Cómo estáis?"; | ||
const buf = Buffer.from(str, "utf8"); | ||
console.log(`buffer byte length: ${buf.byteLength} - string length: ${str.length}`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
"use strict"; | ||
const cp = require("child_process"); | ||
if (process.platform === "win32") | ||
{ | ||
console.log("Unsupported platform!"); | ||
process.exit(0); | ||
} | ||
const ls = cp.spawn("ls", ["-lh", "/tmp"]); | ||
|
||
ls.stdout.on("data", (data) => { | ||
console.log(`stdout: ${data}`); | ||
}); | ||
|
||
ls.stderr.on("data", (data) => { | ||
console.log(`stderr: ${data}`); | ||
}); | ||
|
||
ls.on("close", (code) => { | ||
console.log(`child process exited with code ${code}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
"use strict"; | ||
const cp = require("child_process"); | ||
let ls; | ||
if (process.platform === "win32") | ||
{ | ||
ls = cp.spawn("cmd.exe", ["/c", "dir", process.env["TEMP"]]); | ||
} | ||
else | ||
{ | ||
ls = cp.spawn("ls", ["-lh", "/tmp"]); | ||
} | ||
|
||
ls.stdout.on("data", (data) => { | ||
console.log(`stdout: ${data}`); | ||
}); | ||
|
||
ls.stderr.on("data", (data) => { | ||
console.log(`stderr: ${data}`); | ||
}); | ||
|
||
ls.on("close", (code) => { | ||
console.log(`child process exited with code ${code}`); | ||
}); |
Oops, something went wrong.