Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Got everything to work but http-json-api-server.js #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions baby-steps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(process.argv.slice(2).reduce((a,b) => Number(a) + Number(b)))
15 changes: 15 additions & 0 deletions filtered-ls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const fs = require('fs')

const dirName = process.argv[2]
const fileExtension = process.argv[3]

fs.readdir(dirName,
function callback (err, list) {
if (err) throw err

const re = new RegExp("\." + fileExtension + "$")
const filteredList = list.filter((item) => re.test(item))

console.log(filteredList.join('\n'))
})

1 change: 1 addition & 0 deletions hello-world.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('HELLO WORLD')
8 changes: 8 additions & 0 deletions http-client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const http = require('http')

http.get(process.argv[2], (response) => {
response.setEncoding('utf8')
response.on('data', (data) => {
console.log(data)
})
})
13 changes: 13 additions & 0 deletions http-collect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const http = require('http')
let fullResult = ''

http.get(process.argv[2], (response) => {
response.setEncoding('utf8')
response.on('data', (data) => {
fullResult += data
})
response.on('end', () => {
console.log(fullResult.length)
console.log(fullResult)
})
})
17 changes: 17 additions & 0 deletions http-file-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const http = require('http')
const fs = require('fs')
const port = process.argv[2]
const filePath = process.argv[3]

const server = http.createServer(function listener(req, res) {
const stream = fs.createReadStream(filePath)
let text = ""
stream.on('data', (chunk) => {
text += chunk
})
stream.on('end', () => {
res.end(text)
})
})

server.listen(port)
25 changes: 25 additions & 0 deletions http-json-api-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const http = require('http')
const port = process.argv[2]

const server = http.createServer(function listener(req, res) {
res.writeHead(200, { 'Content-Type': 'application/json' })

const path = req.url.split("?")[0]
const value = req.url.split("=")[1]

const date = new Date(value)

if (path == "/api/parsetime")
res.end(JSON.stringify({hour: date.getHours(),
minute: date.getMinutes(),
second: date.getSeconds()
}))
else if (path == "/api/unixtime")
res.end(JSON.stringify({unixtime: date.getTime()}))
else {
res.writeHead(404)
res.end()
}
})

server.listen(port)
16 changes: 16 additions & 0 deletions http-uppercaser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const http = require('http')
const fs = require('fs')
const port = process.argv[2]
const filePath = process.argv[3]

const server = http.createServer(function listener(req, res) {
let output = ""
req.on('data', (chunk) => {
output += String(chunk).toUpperCase()
})
req.on('end', () => {
res.end(output)
})
})

server.listen(port)
26 changes: 26 additions & 0 deletions juggling-async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const http = require('http')
let fullData = ["", "", ""]
let done = [false, false, false]

function finish() {
console.log(fullData[0])
console.log(fullData[1])
console.log(fullData[2])
}

function callback(response,id) {
response.setEncoding('utf8')
response.on('data', (data) => {
fullData[id] += data
})
response.on('end', () => {
done[id] = true
if (done.every(d => d)) {
finish()
}
})
}

http.get(process.argv[2], (response) => { callback(response,0) })
http.get(process.argv[3], (response) => { callback(response,1) })
http.get(process.argv[4], (response) => { callback(response,2) })
8 changes: 8 additions & 0 deletions make-it-modular.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const mymodule = require('./mymodule')
const fileExtension = process.argv[3]

mymodule(process.argv[2], process.argv[3],
function callback(err, data) {
console.log(data.join('\n'))
}
)
6 changes: 6 additions & 0 deletions my-first-async-io.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const fs = require('fs')
fs.readFile(process.argv[2],'utf8',
function callback (err,data) {
if (err) throw err
console.log(data.match(/\n/g).length)
})
3 changes: 3 additions & 0 deletions my-first-io.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const fs = require('fs')
const file = fs.readFileSync(process.argv[2],'utf8')
console.log(file.match(/\n/g).length)
12 changes: 12 additions & 0 deletions mymodule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = function (dirName, fileExtension, callback) {

const fs = require('fs')

fs.readdir(dirName, function (err, data) {
if (err) return callback(err)

const re = new RegExp("\." + fileExtension + "$")
callback(err, data.filter((item) => re.test(item)))

})
}
15 changes: 15 additions & 0 deletions time-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const net = require('net')
const port = process.argv[2]

const server = net.createServer(function listener(socket) {
const date = new Date()
const dateString = date.getFullYear() + "-" +
(date.getMonth()+1).toString().padStart(2,'0') + "-" +
date.getDate().toString().padStart(2,'0') + " " +
date.getHours().toString().padStart(2,'0') + ":" +
date.getMinutes().toString().padStart(2,'0') + "\n"

socket.end(dateString)
})

server.listen(port)