# Http server #ap6

**URL:** https://forum.codecrafters.io/t/http-server-ap6/641
**Category:** Challenges
**Tags:** challenge:http-server
**Created:** [June 6, 2024, 3:12pm UTC](https://forum.codecrafters.io/t/http-server-ap6/641 "2024-06-06T15:12:05Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Msimbaji](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/msimbaji/32/582_2.png) [@Msimbaji](https://forum.codecrafters.io/u/Msimbaji)
#### Post date: [June 6, 2024, 3:12pm UTC](https://forum.codecrafters.io/t/http-server-ap6/641/1 "2024-06-06T15:12:05Z")

</div>

I’m stuck on Stage #AP6.

I’ve tried everything gpt asked friend but not able to get where i am wrong

Here are my logs:

remote: [tester::#AP6] \>  
remote: [tester::#AP6] Sent bytes: “GET /files/non-existentblueberry\_blueberry\_raspberry\_raspberry HTTP/1.1\r\nHost: localhost:4221\r\n\r\n”  
remote: [tester::#AP6] Failed to read response:  
remote: [tester::#AP6] Received: “” (no content received)  
remote: [tester::#AP6] ^ error  
remote: [tester::#AP6] Error: Expected: HTTP-version, Received: “”  
remote: [tester::#AP6] Test failed  
remote: [tester::#AP6] Terminating program  
remote: [tester::#AP6] Program terminated successfully  
remote:

And here’s a snippet of my code:

```javascript
          else if (url.includes('/files/') && method === "GET") {
            const directory = process.argv[3];
            const filename = url.split("/files/")[1];
            const filePath = `../${directory}/${filename}`;
            
            if (fs.existsSync(filePath)) {
                // Respond with the file content
                const content = fs.readFileSync(filePath).toString();
                const res = `HTTP/1.1 200 OK\r\nContent-Type: application/octet-stream\r\nContent-Length: ${Buffer.byteLength(content)}\r\n\r\n${content}`;
                socket.write(res);
                socket.end();
            } else {
                // Respond with 404 Not Found
                const res = 'HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n';
                socket.write(res);
            }
            
            socket.end();
        }

```

---

<div class="post-metadata">

### Author: ![Msimbaji](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/msimbaji/32/582_2.png) [@Msimbaji](https://forum.codecrafters.io/u/Msimbaji)
#### Post date: [June 7, 2024, 12:54pm UTC](https://forum.codecrafters.io/t/http-server-ap6/641/3 "2024-06-07T12:54:00Z")

</div>

I think there is some problem in the testing system. Today the exact same code passed all the test.

---

<div class="post-metadata">

### Author: ![rohitpaulk](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/rohitpaulk/32/6_2.png) [@rohitpaulk](https://forum.codecrafters.io/u/rohitpaulk)
#### Post date: [June 7, 2024, 4:50pm UTC](https://forum.codecrafters.io/t/http-server-ap6/641/4 "2024-06-07T16:50:38Z")

</div>

I see that the submission that passed was indeed marked as flaky (we haven’t exposed this in the UI yet, but it’s visible to staff internally):

 ![Screenshot 2024-06-07 at 17.45.30](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/1X/4d6f83f3c1cfd68409e096237f0b36fa8a1a46e5.png)

Flakiness is most often caused by a concurrency-related bug, but can also cause due to errors with how CodeCrafters tests/executes your code. We haven’t seen flakiness w/ Javascript submissions on this stage a lot, so I’ll get a team member to take a closer look at this and report back!

---

<div class="post-metadata">

### Author: ![rohitpaulk](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/rohitpaulk/32/6_2.png) [@rohitpaulk](https://forum.codecrafters.io/u/rohitpaulk)
#### Post date: [June 17, 2024, 1:11pm UTC](https://forum.codecrafters.io/t/http-server-ap6/641/6 "2024-06-17T13:11:35Z")

</div>

Hey @Msimbaji,

We were able to identify the issue here. Our starter code contained this block:

```auto
   socket.on("close", () => {
     socket.end();
     server.close(); // This is the problem!
   });

```

This shuts the server down when a connection is closed. This was the first stage where this flow was being triggered, hence the error. We’ve fixed this in the starter code now: [Refactor server code and remove unnecessary server.close() calls by rohitpaulk · Pull Request #74 · codecrafters-io/build-your-own-http-server · GitHub](https://github.com/codecrafters-io/build-your-own-http-server/pull/74).

We’re also changing the tests for the “Concurrent Connections” stage to test this behaviour earlier, instead of waiting till the get/post file stages.

Thank you for highlighting this!

---

<div class="post-metadata">

### Author: ![Msimbaji](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/msimbaji/32/582_2.png) [@Msimbaji](https://forum.codecrafters.io/u/Msimbaji)
#### Post date: [June 18, 2024, 11:04am UTC](https://forum.codecrafters.io/t/http-server-ap6/641/7 "2024-06-18T11:04:52Z")

</div>

Happy to help community. Will i get a certificate for finding bug though 🤡?

---

<div class="post-metadata">

### Author: ![rohitpaulk](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/rohitpaulk/32/6_2.png) [@rohitpaulk](https://forum.codecrafters.io/u/rohitpaulk)
#### Post date: [June 18, 2024, 12:55pm UTC](https://forum.codecrafters.io/t/http-server-ap6/641/8 "2024-06-18T12:55:19Z")

</div>

Since you asked…

 ![DALL·E 2024-06-18 13.54.56 - A certificate design titled 'Bug Finder Certificate' with the text 'Msimbaji' in the recipient's name space. The design includes a playful font header](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/1X/e137deb8cf631afef5b6739b00dcd86e7ded9c49.webp)

---

<div class="post-metadata">

### Author: ![Msimbaji](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/msimbaji/32/582_2.png) [@Msimbaji](https://forum.codecrafters.io/u/Msimbaji)
#### Post date: [June 18, 2024, 1:24pm UTC](https://forum.codecrafters.io/t/http-server-ap6/641/9 "2024-06-18T13:24:01Z")

</div>

Nah 🫡 my interviewer will be happy seeing it in my resume

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)
#### Post date: [June 23, 2024, 1:24pm UTC](https://forum.codecrafters.io/t/http-server-ap6/641/10 "2024-06-23T13:24:45Z")

</div>

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.
