Skip to content

Commit

Permalink
respond with body
Browse files Browse the repository at this point in the history
  • Loading branch information
kripa-sindhu-007 committed Jun 25, 2024
1 parent faf2c02 commit 4c0eea2
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions app/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ const net = require("net");
// You can use print statements as follows for debugging, they'll be visible when running tests.
console.log("Logs from your program will appear here!");


const server = net.createServer((socket) => {
socket.on("data", (data) => {
const request = data.toString();
const url=request.split(' ')[1];
const url = request.split(" ")[1];
if (url == "/") {
socket.write("HTTP/1.1 200 OK\r\n\r\n");
}
else if(url.includes("/echo/")){
} else if (url.includes("/echo/")) {
const content = url.split("/echo/")[1];
const actualLength = Buffer.byteLength(content, "utf8");
socket.write("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: actualLength\r\n\r\n${content}");
const headers = `HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: ${actualLength}\r\n\r\n`;
socket.write(headers + content);
} else {
socket.write("HTTP/1.1 404 Not Found\r\n\r\n");
}
Expand Down

0 comments on commit 4c0eea2

Please sign in to comment.