Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Client server in Dart

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?

like image 863
AomSet Avatar asked Dec 12 '12 15:12

AomSet


People also ask

Is dart client side or server side?

Dart is a Google-produced programming language that has slowly been gaining traction, particularly since its 1.0 release last year. While Dart has thus far been seen as a promising alternative to JavaScript in the browser, I'd like to argue that like Node. js, the true promise of Dart is actually on the server side.

Is dart a client side language?

Dart is a client-optimized language for fast apps on any platform.

Can I use dart in server side?

You can use the dart:io library in command-line scripts, servers, and non-web Flutter apps.


2 Answers

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(".");  
}
like image 121
Chris Buckett Avatar answered Sep 19 '22 05:09

Chris Buckett


To properly serve images, you're going to need to set a Content-Type header. Other than that, the code you have is going in the right direction because it can already serve files. On the other hand, it might be easier to use Apache or Nginx and then setup a reverse proxy to the Dart server. That way Apache or Nginx can serve static files for you. Sorry, we don't yet have all of this documented yet. I also wonder if using Heroku might be a good fit for you.

like image 30
Shannon -jj Behrens Avatar answered Sep 18 '22 05:09

Shannon -jj Behrens