Created
January 30, 2024 17:55
-
-
Save BRAVO68WEB/5b3b12596bbf069024c62455908df872 to your computer and use it in GitHub Desktop.
Day 1
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 net = require('net'); | |
const { URL } = require('url'); | |
const io = new net.Server(); | |
io.on('connection', (connection) => { | |
console.log('Client connected') | |
connection.on('data', (info) => { | |
const requestData = info.toString().split('\n'); | |
const requestUrl = requestData[0].split(' ')[1]; | |
if (requestUrl == '/hi') { | |
connection.write('Hello'); | |
} | |
else if(requestUrl.startsWith('/add')){ | |
let query = requestUrl.split('?')[1] | |
let args = query.split('&') | |
let argList = {} | |
for (const arg of args) { | |
let [key, value] = arg.split('=') | |
argList[key] = value | |
} | |
let nosToAdd = Object.values(argList) | |
let sum = nosToAdd.reduce((a, b) => parseInt(a) + parseInt(b), 0); | |
connection.write(String(sum)); | |
} | |
else { | |
connection.write('404 Not Found'); | |
} | |
connection.destroy(); | |
}) | |
}) | |
io.listen(1234); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment