Skip to content

Instantly share code, notes, and snippets.

@BRAVO68WEB
Created January 30, 2024 17:55
Show Gist options
  • Save BRAVO68WEB/5b3b12596bbf069024c62455908df872 to your computer and use it in GitHub Desktop.
Save BRAVO68WEB/5b3b12596bbf069024c62455908df872 to your computer and use it in GitHub Desktop.
Day 1
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