listen method
Implementation
void listen({InternetAddress? host, int port = 4040}) async {
host ??= InternetAddress.anyIPv4;
_http = await HttpServer.bind(host, port, shared: true);
await for (final HttpRequest req in _http!) {
final context = new Context(req, logger: logger);
context.log?.info('request is started');
if (req.method == HttpMethod.post ||
req.method == HttpMethod.put ||
req.method == HttpMethod.patch) {
final contentType = req.headers.contentType != null
? new MediaType.parse(req.headers.contentType.toString())
: null;
final body = await utf8.decodeStream(req);
if (contentType?.mimeType == 'application/json') {
final jsonval = jsonDecode(body) as Map<String, dynamic>;
context.req.body = jsonval;
}
}
try {
await middlewareChain.execute(context);
} catch (e) {
context.res.statusCode = 500;
context.res.write('Internal server error');
}
if (!context.res.isClosed()) {
context.res.close();
}
context.log?.info('request is finished');
}
}