Skip to content
This repository has been archived by the owner on Oct 25, 2018. It is now read-only.

Commit

Permalink
add node.js examples
Browse files Browse the repository at this point in the history
  • Loading branch information
darthcav committed Dec 8, 2016
1 parent 903d2fd commit 6b8836b
Show file tree
Hide file tree
Showing 59 changed files with 918 additions and 0 deletions.
13 changes: 13 additions & 0 deletions _expressjs/hello-world.js
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}`);
});
2 changes: 2 additions & 0 deletions _nodejs/01a-hello-world/hello-world.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"use strict";
console.log("Hello World!");
15 changes: 15 additions & 0 deletions _nodejs/01b-hello-world-server/hello-world-server.js
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}/`);
});
14 changes: 14 additions & 0 deletions _nodejs/02a-timers/timer.js
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);
17 changes: 17 additions & 0 deletions _nodejs/03a-console/time.js
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);
3 changes: 3 additions & 0 deletions _nodejs/04a-modules/main-test.js
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}`);
11 changes: 11 additions & 0 deletions _nodejs/04b-modules/module.js
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);
});
26 changes: 26 additions & 0 deletions _nodejs/05a-os/system.js
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));
22 changes: 22 additions & 0 deletions _nodejs/06a-events/my-emitter-driver.js
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");
}
5 changes: 5 additions & 0 deletions _nodejs/06a-events/my-emitter.js
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 {};
11 changes: 11 additions & 0 deletions _nodejs/06b-events/my-emitter-driver.js
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!"));
5 changes: 5 additions & 0 deletions _nodejs/06b-events/my-emitter.js
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 {};
10 changes: 10 additions & 0 deletions _nodejs/06c-events/my-emitter-driver.js
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");
}
23 changes: 23 additions & 0 deletions _nodejs/06c-events/my-emitter.js
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!");
});
}
};
13 changes: 13 additions & 0 deletions _nodejs/07a-errors/error1.js
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);
}
13 changes: 13 additions & 0 deletions _nodejs/07a-errors/error2.js
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!");
}
});
8 changes: 8 additions & 0 deletions _nodejs/07a-errors/error3.js
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);
});
15 changes: 15 additions & 0 deletions _nodejs/07b-errors/custom-error.js
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";
}
};
13 changes: 13 additions & 0 deletions _nodejs/07b-errors/error1.js
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);
}
32 changes: 32 additions & 0 deletions _nodejs/08a-process/process1.js
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 #####");
}
7 changes: 7 additions & 0 deletions _nodejs/08a-process/process2.js
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));
16 changes: 16 additions & 0 deletions _nodejs/08a-process/process3.js
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();
8 changes: 8 additions & 0 deletions _nodejs/09a-buffer/buffer1.js
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)}`);
}
8 changes: 8 additions & 0 deletions _nodejs/09a-buffer/buffer2.js
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)}`);
}
5 changes: 5 additions & 0 deletions _nodejs/09a-buffer/buffer3.js
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}`);
20 changes: 20 additions & 0 deletions _nodejs/10a-child_process/cp1.js
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}`);
});
23 changes: 23 additions & 0 deletions _nodejs/10a-child_process/cp2.js
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}`);
});
Loading

0 comments on commit 6b8836b

Please sign in to comment.