lite_server 0.2.3 copy "lite_server: ^0.2.3" to clipboard
lite_server: ^0.2.3 copied to clipboard

Lite Http Server for Dart

example/main.dart

import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:isolate';
import 'package:lite_server/lite_server.dart';
import 'package:lite_server/src/lite_server.dart';

import 'routes/home.dart';

void main(List<String> arguments) async {
  // LiteServerLogger.instance.cleanLogs();

  final liteServer = LiteServer(
    routes: [
      homeRoute,
      HttpRoute.get(
        '/',
        handler: (request, payload) {
          // throw Exception('Error test');
          // // request.response.redirect(Uri(path: '/api/users'));
          final cwd = Directory.current.path;
          request.response.file('$cwd/assets/web/images/512.png');
        },
        routes: [
          HttpRoute.get(
            'api',
            routes: [
              HttpRoute.get(
                'users',
                handler: (request, payload) {
                  request.response.json(<Object>[]);
                },
              ),
            ],
          ),
        ],
      ),
      HttpRoute.post(
        '/user/<id>',
        handler: (request, payload) async {
          print(jsonDecode(await request.readBodyAsString()));
          await request.response.json(payload.pathParameters);
        },
      ),
      HttpRoute.get(
        '/user/<id>',
        handler: (request, payload) async {
          await request.response.json(payload.pathParameters);
        },
        routes: [
          HttpRoute.get(
            '<pack>',
            handler: (request, payload) async {
              await request.response.json(payload.pathParameters);
            },
            routes: [
              HttpRoute.get(
                '<dept>',
                handler: (request, payload) async {
                  await request.response.json(payload.pathParameters);
                },
              ),
            ],
          ),
        ],
      ),
      HttpRoute.post(
        '/post',
        handler: (request, payload) async {
          print(jsonDecode(await request.readBodyAsString()));
          await request.response.ok('posted');
        },
      ),
      HttpRoute.post(
        '/upload',
        handler: (request, payload) async {
          await for (final entry in request.multipartData()) {
            print(entry.info);

            if (!entry.info.containsKey('content-type')) {
              print(utf8.decode(entry.bytes));
            }
          }

          await request.response.ok('uploaded');
        },
      ),
      HttpStaticRoute(
        '/images',
        directoryPath: 'assets/images/',
        listDirectory: true,
      ),
      HttpStaticRoute(
        '/web',
        directoryPath: 'assets/web/',
        defaultDocument: 'index.html',
      ),
    ],
  );

  for (var i = 0; i < 6; i++) {
    await Isolate.spawn(startServer, liteServer);
  }

  // print(liteServer.routeMap.keys.join('\n'));
  // await startServer(liteServer);

  await Future<void>.delayed(const Duration(seconds: 500));
}

Future<void> startServer(LiteServer liteServer) async {
  final server = await HttpServer.bind(
    InternetAddress.anyIPv4,
    9080,
    shared: true,
  )
    ..autoCompress = true
    ..serverHeader = Isolate.current.hashCode.toString();

  liteServer.listen(
    server,
    controllers: [
      LoggerController(level: LogLevel.errors),
      CorsOriginController(
        allowedMethods: {'GET', 'POST', 'OPTIONS'},
      ),
    ],
  );
}
1
likes
130
points
27
downloads

Publisher

verified publishergece.dev

Weekly Downloads

Lite Http Server for Dart

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

mime

More

Packages that depend on lite_server