Client server in Dart

client-server, dart

Solution

I've pasted your code (and edited it slightly, I think there's a couple of typos), and it does serve images in chrome - at present, you would have to pass the full url of the image, eg: `http://localhost:1111/images/foo.png`

To get a page full of images, you would either need to write an html page, eg:

<html><body>
   <img src="http://localhost:1111/images/foo.png"/>
   <img src="http://localhost:1111/images/bar.png"/>
</body></html>

And there's no reason why you couldn't create that html dynamically on the server, for example, in response to a request for a file called `images.html` for example. Take a look at the `DirectoryLister` class to iterate files and folders on the server side.

Also, JJ's comment is also correct - you should also add proper headers, (although chrome seems to pretty good at interpreting stuff without the proper headers).

For reference, here's the server side code that works fine for me (just so that I could test it... - has the 404 and options removed - it serves from the current (ie, app's own) folder).

import 'dart:io';

void startServer(String mainPath){
  HttpServer server = new HttpServer();
  server.listen('127.0.0.1', 1111);
  print("Server listening on localhost, port 1111");

  server.defaultRequestHandler = (var req, var res) {
    final String path = req.path == '/' ? '/index.html' : req.path;
    final File file = new File('${mainPath}${path}');

    file.exists().then((bool found) {
      if(found) {
        file.fullPath().then((String fullPath) {
          file.openInputStream().pipe(res.outputStream);
        });
      }
    });      
  };
}

main(){
   startServer(".");  
}

Problem

I have found a few nice tutorials on the client/server in Dart. The client just makes a request to the server via localhost on the specified port, and the server just responds with a String. However, I did not find any help on how to serve images. I want to be able to get the server to server images to the client. For instance, if the client does a request like: localhost:1313/Images, then the server should respond with a page displaying all the images that are in the "images" folder. Here is the code I have so far: ``` import 'dart:io'; class Server { _send404(HttpResponse res){ res.statusCode = HttpStatus.NOT_FOUND; res.outputStream.close(); } void startServer(String mainPath){ HttpServer server = new HttpServer(); server.listen('localhost', 1111); print("Server listening on localhost, port 1111"); server.defaultRequestHandler = (var req, var res) { final String path = req.path == '/' ? '/index.html' : req.path; final File file = new File('${mainPath}${path}'); file.exists().then((bool found) { if(found) { file.fullPath().then((String fullPath) { if(!fullPath.startsWith(mainPath)) { _send404(res); } else { file.openInputStream().pipe(res.outputStream); } }); } else { _send404(res); } }); }; void main(){ Server server = new Server(); File f = new File(new Options().script); f.directory().then((Directory directory) { server.startServer(directory.path); }); } ``` I have not yet implemented the client, but is it necessary to implement a client? Isn't the browser enough as client? Also, what do I need to do to make the server serve the images?

Original source