Skip to content

Commit f623014

Browse files
author
Robert Jackson
authored
Migrate main implementation to leveraging async/await.
1 parent 996604d commit f623014

1 file changed

Lines changed: 27 additions & 35 deletions

File tree

src/index.js

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,46 +25,38 @@ function fastbootExpressMiddleware(distPath, options) {
2525
});
2626
}
2727

28-
return function(req, res, next) {
28+
return async function(req, res, next) {
2929
let path = req.url;
30-
fastboot.visit(path, { request: req, response: res }).then(success, failure);
3130

32-
function success(result) {
31+
try {
32+
let result = await fastboot.visit(path, { request: req, response: res })
3333
let responseBody = opts.chunkedResponse ? result.chunks() : result.html();
3434

35-
responseBody
36-
.then(body => {
37-
let headers = result.headers;
38-
let statusMessage = result.error ? 'NOT OK ' : 'OK ';
39-
40-
for (var pair of headers.entries()) {
41-
res.append(pair[0], pair[1]);
42-
}
43-
44-
if (result.error) {
45-
log('RESILIENT MODE CAUGHT:', result.error.stack);
46-
next(result.error);
47-
}
48-
49-
log(result.statusCode, statusMessage + path);
50-
res.status(result.statusCode);
51-
52-
if (typeof body === 'string') {
53-
res.send(body);
54-
} else if (result.error) {
55-
res.send(body[0]);
56-
} else {
57-
body.forEach(chunk => res.write(chunk));
58-
res.end();
59-
}
60-
})
61-
.catch(error => {
62-
res.status(500);
63-
next(error);
64-
});
65-
}
35+
let body = await responseBody;
36+
let headers = result.headers;
37+
let statusMessage = result.error ? 'NOT OK ' : 'OK ';
38+
39+
for (var pair of headers.entries()) {
40+
res.append(pair[0], pair[1]);
41+
}
42+
43+
if (result.error) {
44+
log('RESILIENT MODE CAUGHT:', result.error.stack);
45+
next(result.error);
46+
}
47+
48+
log(result.statusCode, statusMessage + path);
49+
res.status(result.statusCode);
6650

67-
function failure(error) {
51+
if (typeof body === 'string') {
52+
res.send(body);
53+
} else if (result.error) {
54+
res.send(body[0]);
55+
} else {
56+
body.forEach(chunk => res.write(chunk));
57+
res.end();
58+
}
59+
} catch (error) {
6860
if (error.name === 'UnrecognizedURLError') {
6961
next();
7062
} else {

0 commit comments

Comments
 (0)