Site-www: Tutorial 'Write HTTP clients & servers' doesn't work as expected

Created on 19 Sep 2020  ·  10Comments  ·  Source: dart-lang/site-www

hello_world_server.dart doesn't work as expected.

Problem

The Write HTTP clients & servers tutorial in the Using the http_server package section doesn't work as expected.

Steps to reproduce

Download the example code or go to 'Get the source code' section and download it.

Follow the steps from the 'Using the http_server package' section.


Steps from 'Run the hello world server' if you don't want to open another tab

  1. At the command line, run the hello_world_server.dart script:
cd httpserver
dart bin/hello_world_server.dart
listening on localhost, port 4040
  1. In any browser, visit localhost:4040. The browser displays Hello, world!

What I'm getting instead?

It hangs forever:

curl "http://localhost:4044/"
[] <- This simulates the cursor


Browser Image

Screen Shot 2020-09-18 at 7 14 19 PM

I used curl too to demonstrate is not a browser issue.

beginner help wanted needs-info

All 10 comments

Thanks for filing the issue, @Classy-Bear. Could you please provide the output of dart --version?

Also, can anyone else try this and tell me what you see? Thanks!

Sure, here it is: Dart SDK version: 2.9.1 (stable) (Wed Aug 12 12:37:19 2020 +0200) on "macos_x64"
NOTE: I haven't test with other SDK version.

Let me know if I can help with this issue!

I've tried downloading the suggested example HTTP server project above and was able to successfully access a return via curl http://127.0.0.1:4040/ running from Dart SDK version: 2.9.1 (stable) (Wed Aug 12 12:37:19 2020 +0200) on "macos_x64" returning the result of "Hello, world!"

Please make sure you are accessing the correct port and the port is not currently occupied?

curl "http://localhost:4044/" is different than curl "http://127.0.0.1:4040/"

Happy Hacktoberfest! 👻

It looks like I point to the wrong example. The problem is in this section: Using the http_server package. @stevenanthonyrevo Can you run the new example? The new example is not related to the http_server package just to be clear.

Correct, the downloadable zip with the examples is an outdated bundle of the master (archived).
Archived: https://github.com/dart-lang/dart-tutorials-samples/blob/master/httpserver/bin/mini_file_server.dart
Newer: https://github.com/dart-lang/site-www/blob/master/examples/httpserver/bin/mini_file_server.dart

Stream<HttpRequest> server; is missing. I will write the correct example file down below and then submit a PR to the archived example.

````
import 'dart:async';
import 'dart:io';

import 'package:path/path.dart' as p;

var targetFile =
File(p.join(p.dirname(Platform.script.toFilePath()), 'index.html'));

Future main() async {
var server;

try {
server = await HttpServer.bind(InternetAddress.loopbackIPv4, 4044);
} catch (e) {
print("Couldn't bind to port 4044: $e");
exit(-1);
}
print('Listening on http://localhost:4044/');

await for (HttpRequest req in server) {
if (await targetFile.exists()) {
print("Serving ${targetFile.path}.");
req.response.headers.contentType = ContentType.html;
try {
await req.response.addStream(targetFile.openRead());
} catch (e) {
print("Couldn't read file: $e");
exit(-1);
}
} else {
print("Can't open ${targetFile.path}.");
req.response.statusCode = HttpStatus.notFound;
}
await req.response.close();
}
}
````

This pull request doesn't add any value. You're just specifying a type annotation, which is good because it fulfills the following lint: prefer_typing_uninitialized_variables, but doesn't actually solves the problem.

Can you describe “...but doesn’t actually solve the problem?” With more detail.

Have you installed the needed package “Path” using ‘pub get’ ?

This resolved any issues I seen within the repo.

Would need more details to investigate further

On Sep 26, 2020, at 12:35 AM, David Acevedo notifications@github.com wrote:



This pull request doesn't add any value. You're just specifying a type annotation, which is good because it fulfills the following lint: prefer_typing_uninitialized_variableshttps://dart-lang.github.io/linter/lints/prefer_typing_uninitialized_variables.html, but doesn't actually solves the problem.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHubhttps://github.com/dart-lang/site-www/issues/2646#issuecomment-699387853, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ACA4PTLZVZSZCLACALWFAALSHVVRHANCNFSM4RSUSP3A.

Can you describe “...but doesn’t actually solve the problem?” With more detail. Have you installed the needed package “Path” using ‘pub get’ ? This resolved any issues I seen within the repo. Would need more details to investigate further On Sep 26, 2020, at 12:35 AM, David Acevedo notifications@github.com wrote:  This pull request doesn't add any value. You're just specifying a type annotation, which is good because it fulfills the following lint: prefer_typing_uninitialized_variableshttps://dart-lang.github.io/linter/lints/prefer_typing_uninitialized_variables.html, but doesn't actually solves the problem. — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub<#2646 (comment)>, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ACA4PTLZVZSZCLACALWFAALSHVVRHANCNFSM4RSUSP3A.

As I say, fixing a bad lint doesn't solve the problem. You're just giving a variable a type. The example still doesn't work if you add this line: Stream<HttpRequest> server;

After reading carefully the Write HTTP clients & servers page I got to solved. The response wasn't closed, so I add await req.response.close();.

Does this issue still needs some work?

No, since the response is being closed, the code from this repo is up to date and the example code in the "Write HTTP clients & servers" is correct.

Was this page helpful?
0 / 5 - 0 ratings