dart_express 0.2.0

  • Readme
  • Changelog
  • Example
  • Installing
  • new50

An express-like web server framework for Dart developers.

Created from templates made available by Stagehand under a BSD-style license.

Usage #

A simple usage example:

import 'package:dart_express/dart_express.dart';

main() {
  var app = express();

  app.get('/', (req, res, _) {
    res.json({
      'hello': 'world',
      'test': true,
    });
  });

  app.listen(3000, (port) => print('Listening on port $port');
}

Example with route parameters

import 'package:dart_express/dart_express.dart';

main() {
  var app = express();

  app.get('/users/:userId/posts/:postId', (req, res, _) {
    res.json({
      'userId': req.params['userId'],
      'postId': req.params['postId'],
    });
  });

  app.listen(3000, (port) => print('Listening on port $port');
}

With Body parsing Middleware:

import 'package:dart_express/dart_express.dart';

main() {
  var app = express();

  app.use(BodyParser.json());

  app.post('/post', (req, res, _) {
    print(req.body);

    res.send({
      'request_body': req.body,
    });
  });

  app.listen(3000, (port) => print('Listening on port $port');
}

Using the mustache templating engine

import 'package:dart_express/dart_express.dart';

main() {
  var app = express();

  app.use(BodyParser.json());
  app.engine(MustacheEngine.use());

  app.settings.viewsPath = 'custom_views_path';
  app.settings.viewEngine = 'mustache';

  app.get('/', (req, res, _) {
    res.render('index', {
      'app_name': 'My Test App',
    });
  });

  app.listen(3000, (port) => print('Listening on port $port');
}

Currently supported View Engines #

  • Basic HTML
  • Mustache
  • Jael

Roadmap #

  • [X] Basic Routing
  • [X] Easily build Middleware
  • [X] Add & use view engines easily
  • [ ] Add in-depth testing
  • [ ] Clean up imports and extract middleware to separate packages
  • [ ] Add Dart "morgan" middleware package for logging HTTP requests
  • [ ] Add CORS middleware package

0.1.0 #

  • Initial version of Dart express with basic functionality for routing and middleware

0.1.1 #

  • Add dependencies for Jael (will be breaking this out to a separate package later)

0.2.0 #

  • Add path_to_regexp package to improve route matching.
  • Add support for route parameters using path_to_regexp package.
  • Improve README to include additional documentation
  • Add res.end() and res.location() methods to Response class.

example/dart_express_example.dart

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

const int PORT = 5000;

main() {
  var app = express();

  app.use(BodyParser.json());

  app.engine(MustacheEngine.use());
  app.engine(JaelEngine.use());
  app.settings.viewEngine = 'mustache';

  app.get('/', (req, res, _) {
    res.statusCode = HttpStatus.ok;

    res.json({
      'hello': 'world',
      'age': 25,
    });
  });

  app.all('/secret', (req, res, next) {
    print('Accessing the secret section');

    next();
  });

  app.get('/secret', (Request req, Response res, next) {
    res.send('Secret Home Page');
  });

  app.get('/2', (req, res, _) {
    res.render('index');
  });

  app.get('/3', (req, res, _) {
    res.render('about', {
      'first_name': req.params['first_name'] ?? 'Devin Riegle',
      'person': req.params['person']
    });
  });

  app.get('/4', (req, res, _) {
    res.render(
        'test.jl', {'template_engine': 'Jael', 'first_name': 'Thosakwe'});
  });

  app.post('/post', (Request req, Response res, _) {
    res.send('Data from post :)');
  });

  app.get('/users/:userId/posts/:postId', (req, res, _) {
    print(req.params);

    res.render('test.jl', {
      'template_engine': req.params['postId'],
      'first_name': 'George',
    });
  });

  app.listen(PORT, (int port) => print('Listening on port $port'));
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  dart_express: ^0.2.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:dart_express/dart_express.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
50
Learn more about scoring.

We analyzed this package on Nov 11, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.6.0
  • pana: 0.12.21

Platforms

Detected platforms: other

Primary library: package:dart_express/dart_express.dart with components: io, mirrors.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.3.0 <3.0.0
code_buffer ^1.0.1 1.0.1
http ^0.12.0+2 0.12.0+2
jael ^2.0.2 2.0.2
meta ^1.1.7 1.1.8
mustache4dart ^3.0.0-dev.0.0 3.0.0-dev.1.0
path ^1.6.4 1.6.4
path_to_regexp ^0.2.1 0.2.1
symbol_table ^2.0.0 2.0.0
Transitive dependencies
analyzer 0.38.5 0.39.1
analyzer_plugin 0.2.1
args 1.5.2
async 2.4.0
build 1.2.1
build_config 0.4.1+1
build_daemon 2.1.0
build_resolvers 1.2.1 1.2.2
build_runner 1.7.2
build_runner_core 4.1.0
built_collection 4.2.2
built_value 6.8.2 7.0.0
built_value_generator 6.8.2 7.0.0
charcode 1.1.2
checked_yaml 1.0.2
code_builder 3.2.0
collection 1.14.12
convert 2.1.1
crypto 2.1.3
csslib 0.16.1
dart_style 1.3.3
fixnum 0.10.11
front_end 0.1.27 0.1.29
glob 1.2.0
graphs 0.2.0
html 0.14.0+3
http_multi_server 2.1.0
http_parser 3.1.3
io 0.3.3
js 0.6.1+1
json_annotation 3.0.0
kernel 0.3.27 0.3.29
logging 0.11.3+2
matcher 0.12.6
mime 0.9.6+3
node_interop 1.0.3
node_io 1.0.1+2
package_config 1.1.0
package_resolver 1.0.10
pool 1.4.0
pub_semver 1.4.2
pubspec_parse 0.1.5
quiver 2.1.2+1
reflectable 2.2.0
shelf 0.7.5
shelf_web_socket 0.2.3
source_gen 0.9.4+6
source_span 1.5.5
stack_trace 1.9.3
stream_channel 2.0.0
stream_transform 0.0.20
string_scanner 1.0.5
term_glyph 1.1.0
timing 0.1.1+2
typed_data 1.1.6
watcher 0.9.7+12
web_socket_channel 1.1.0
yaml 2.2.0
Dev dependencies
pedantic ^1.8.0 1.8.0+1
test ^1.6.0