sevr 1.0.4 copy "sevr: ^1.0.4" to clipboard
sevr: ^1.0.4 copied to clipboard

A library to easily create http servers with Dart and perform Http requests.

example/example.dart

import 'dart:io';

import 'package:sevr/sevr.dart';
import 'package:path/path.dart' as p;

dynamic main() {
  var serv = Sevr();

  //let sevr know to serve from the /web directory
  serv.use(Sevr.static('example/web'));

  //Allowe cross Origin requests
  serv.use(CORS(['*']));

  //Use path to get directory of the files to serve on that route
  serv.get('/serve', [
    (ServRequest req, ServResponse res) {
      
      return res.status(200).sendFile(p.absolute('example/web/index.html'));
    }
  ]);

  //get request
  serv.get('/', [
    (ServRequest req, ServResponse res) {
      return res.status(200).send('<h1>Hello World</h1>');
    }
  ]);

  //post request
  serv.post('/post', [
    (ServRequest req, ServResponse res) async {
      return res.status(200).json(req.body);
    }
  ]);

  //plain text
  serv.get('/text', [
    (ServRequest req, ServResponse res) {
      return res.status(200).send('data');
    }
  ]);

  // request parameters
  serv.get('/param/:username', [
    (ServRequest req, ServResponse res) {
      return res.status(200).json({'params': req.params});
    }
  ]);

  // query parameters
  serv.get('/query', [
    (ServRequest req, ServResponse res) {
      return res.status(200).json(req.query);
    }
  ]);

  //Upload Files
  serv.post('/upload', [
    (req, res) async {
      for (var i = 0; i < req.files.keys.length; i++) {
        //Handle your file stream as you see fit, write to file, pipe to a cdn etc --->
        var file = File(req.files[req.files.keys.toList()[i]].filename);
        await for (var data
            in req.files[req.files.keys.toList()[i]].streamController.stream) {
          if (data is String) {
            await file.writeAsString(data, mode: FileMode.append);
          } else {
            await file.writeAsBytes(data, mode: FileMode.append);
          }
        }
      }

      return res.status(200).json(req.body);
    }
  ]);

  //Bind server to port 4000
  serv.listen(4000, callback: () {
    print('Listening on port: ${4000}');
  });
}
16
likes
40
pub points
8%
popularity

Publisher

unverified uploader

A library to easily create http servers with Dart and perform Http requests.

Repository (GitHub)
View/report issues

License

BSD-2-Clause (license)

Dependencies

body_parser, dart2_constant, mime, path, pedantic

More

Packages that depend on sevr