commit 0d978dde5aa1cf9fc4fc0957eadd847cab251a8e Author: mrshitovsasha Date: Wed Apr 26 19:33:05 2023 +0300 Lab2_26042023 diff --git a/SOFIA.html b/SOFIA.html new file mode 100644 index 0000000..dd70917 --- /dev/null +++ b/SOFIA.html @@ -0,0 +1,16 @@ + + + + + + + SOFIA + + + + + + + + + diff --git a/files.js b/files.js new file mode 100644 index 0000000..e8a03bf --- /dev/null +++ b/files.js @@ -0,0 +1,28 @@ +const http = require('http'); + const fs = require('fs'); + + const server = http.createServer((req, res) => { + //console.log(req) + + + fs.readFile('./'+req.url, (err, data) => { + if(err){ + res.writeHead(404, "Not Found"); + res.write("No file found"); + res.end(); + } else { + res.writeHead(200, { + 'Content-Length': data.length, + 'Content-Type': 'text/html' + }) + res.write(data); + res.end(); + } + }); + }); + server.on('clientError', (err, socket) => { + socket.end('HTTP/1.1 400 Bad Request\r\n\r\n'); + }); + server.listen(8088, () => { + console.log("listen on http://localhost:8088/"); + }); \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..5ffbbbb --- /dev/null +++ b/index.js @@ -0,0 +1,28 @@ +const net = require('net'); + const fs = require('fs'); + const server = net.createServer((c) => { + // 'connection' listener. + console.log('client connected'); + c.on('end', () => { + console.log('client disconnected'); + }); + //c.write('HTTP/1.1 200 OK\r\nContent-Length: 12\r\nContent-Type: text/plain; charset=utf-8\r\n\r\nHello World!'); + fs.readFile('./SOFIA.html', (err, data) => { + c.write('HTTP/1.1 200 OK\r\n'); + c.write('Content-Length: '+data.length+'\r\n'); + c.write('Content-Type: text/html; charset=utf-8\r\n'); + c.write('\r\n'); + c.write(data); + }); + + //c.pipe(c); + c.on('data', (data) => { + console.log(data.toString()); + }) + }); + server.on('error', (err) => { + throw err; + }); + server.listen(8124, () => { + console.log('server bound'); + });