HelpfulPanda
HelpfulPanda

Reputation: 377

How can I use the http_server package to serve several virtual web apps in Dart?

In Dart language, the http_server package allows to implement virtual hosts.

import 'package:http_server/http_server.dart';
import 'dart:io';

void main() {

   HttpServer.bind('localhost', 8080).then((server) {
        var virtualServer = new VirtualHost(server);
          virtualServer.addHost('domain1.com').listen(
             (HttpRequest request) {
                //  what should I do now?
             }
   });

}
  1. How can I serve a web site in a subdirectory below /web/ using the http_server package?
  2. Is it best to place the web sites below the usual "web" directory?

Upvotes: 1

Views: 345

Answers (1)

Alexandre Ardhuin
Alexandre Ardhuin

Reputation: 76223

You can do :

import 'dart:io';

import 'package:http_server/http_server.dart';

void main() {
  HttpServer.bind('localhost', 8080).then((server) {
    final virtualServer = new VirtualHost(server);
    final domain1Stream = virtualServer.addHost('domain1.com');
    new VirtualDirectory('/var/www/domain1').serve(domain1Stream);
  });
}

Upvotes: 1

Related Questions