Created
March 25, 2020 04:04
-
-
Save ObjSal/34f0617edd71d059f8822ddccb76e3c2 to your computer and use it in GitHub Desktop.
Node Playground used while learning Node.js
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
// I created this script as a playground when learning node.js | |
// The Majority of examples are grabbed from https://nodejs.dev/ | |
/** | |
* Make this server reacheable by the world. | |
* Install ngrok and type `ngrok PORT` and the PORT you want is exposed to the | |
* internet. You will get a ngrok.io domain, but with a paid subscription you | |
* can get a custom URL as well as more security options. | |
* | |
* Another service you can use is https://github.com/localtunnel/localtunnel | |
*/ | |
const http = require('http'); | |
const hostname = '127.0.0.1'; | |
const port = 3000; /* ¿process.env.PORT? */ | |
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}/`); | |
}); | |
// process does not require a "require", it's automatically available. | |
// I get 'undefined' when I run just `node app.js` | |
// I have to manually send the value using `NODE_ENV=debug node app.js` or | |
// it will probably work if its exported as an environment variable. | |
console.log(`NODE_END: ${process.env.NODE_ENV}`) | |
// SIGTERM is the signal that tells a process to gracefully terminate. | |
// It is the signal that's sent from process managers like upstart or | |
// supervisord and many others. | |
// It can be invoked by calling `process.kill(process.pid, 'SIGTERM')` | |
process.on('SIGTERM', () => { | |
server.close(() => { | |
console.log('Process terminated') | |
}) | |
}) | |
// Print how many oranges and apples are printed | |
const oranges = ['orange', 'orange'] | |
const apples = ['just one apple'] | |
oranges.forEach(fruit => { | |
console.count(fruit) | |
}) | |
apples.forEach(fruit => { | |
console.count(fruit) | |
}) | |
// Print the call stack trace | |
const function2 = () => console.trace() | |
const function1 = () => function2() | |
function1() | |
// Calculate how much time a function takes to run, using time() and timeEnd() | |
const doSomething = () => console.log('test') | |
const measureDoingSomething = () => { | |
console.time('doSomething()') | |
//do something, and measure the time it takes | |
doSomething() | |
console.timeEnd('doSomething()') | |
} | |
measureDoingSomething() | |
// Print to the error log (or console) | |
console.error('hmm') | |
// Prints hi! in yellow | |
// low level way: console.log('\x1b[33m%s\x1b[0m', 'hi!') | |
// Instead using an easy to use library called chalk | |
// $ npm install chalk | |
const chalk = require('chalk') | |
console.log(chalk.yellow('hi!')) | |
// Create a 10-step progress bar and every 100ms one step is | |
// completed. When the bar completes it clears the interval | |
// Install a package called progress | |
// $ npm install progress | |
const ProgressBar = require('progress') | |
const bar = new ProgressBar(':bar', { total: 10 }) | |
const timer = setInterval(() => { | |
bar.tick() | |
if (bar.complete) { | |
clearInterval(timer) | |
} | |
}, 100) | |
// Accept input, process and print out the result in this case a greeting | |
const readline = require('readline').createInterface({ | |
input: process.stdin, | |
output: process.stdout | |
}) | |
readline.question(`What's your name?`, name => { | |
console.log(`Hi ${name}!`) | |
readline.close() | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment