Last active
December 21, 2018 22:46
-
-
Save bhuizi/4414b84beee354cc3df6208a51ee0990 to your computer and use it in GitHub Desktop.
learn_node
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
const fs = require('fs'); | |
const path = require('path'); | |
const http = require('http'); | |
const read = require('./read'); | |
/*1. HELLO WORLD */ | |
//console.log('HELLO WORLD'); | |
/*2. BABY STEPS process.argv */ | |
// const newArray = process.argv.slice(2, process.argv.length); | |
// const sum = newArray.reduce((acc, next) => { | |
// acc = parseInt(acc) + parseInt(next); | |
// return acc; | |
// }, 0); | |
// console.log(sum); | |
/*3. MY FIRST I/O! */ | |
// console.log(fs.readFileSync(process.argv[2]).toString().split('\n').length -1); | |
// console.log(fs.readFileSync(process.argv[2], 'utf-8').split('\n').length -1); | |
/*4. MY FIRST ASYNC I/O! */ | |
// fs.readFile(process.argv[2], 'utf-8', (err, data) => { | |
// if(err) console.log(err); | |
// console.log(data.split('\n').length - 1); | |
// }); | |
/*5. FILTERED LS */ | |
// const dir = process.argv[2]; | |
// const ext = process.argv[3]; | |
// fs.readdir(dir, (err, list) => { | |
// if(err) console.log(err); | |
// list.map(item => { | |
// if(path.extname(item) === `.${ext}`) { | |
// console.log(item); | |
// } | |
// }) | |
// }) | |
/*6. MAKE IT MODULAR */ | |
// call | |
// read( | |
// process.argv[2], | |
// process.argv[3], | |
// function(err, data) { | |
// err ? console.log(err) : data.map(d => console.log(d)); | |
// } | |
// ) | |
//module | |
// const fs = require('fs'); | |
// const path = require('path'); | |
// module.exports = (dir, ext, cb) => | |
// fs.readdir(dir, (err, list) => { | |
// if(err) { | |
// return cb(err) | |
// } | |
// let result = list.filter(item => { | |
// if(path.extname(item) === `.${ext}`) { | |
// return item; | |
// } | |
// }) | |
// return cb(null, result); | |
// }) | |
/*7. HTTP CLIENT */ | |
http.get(process.argv[2], (res) => { | |
res.setEncoding('utf8'); | |
res.on('data', (data) => console.log(data)) | |
}).on('error', (e) => console.log(e)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment