Note that there are some explanatory texts on larger screens.

plurals
  1. POClient server in Dart
    primarykey
    data
    text
    <p>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.</p> <p>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. </p> <p>Here is the code I have so far:</p> <pre class="lang-dart prettyprint-override"><code>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); }); } </code></pre> <p>I have not yet implemented the client, but is it necessary to implement a client? Isn't the browser enough as client?</p> <p>Also, what do I need to do to make the server serve the images?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload