Skip to content

Commit 4c4f58b

Browse files
author
Hyunje Jun
committed
Doc update for #25
1 parent b966568 commit 4c4f58b

File tree

2 files changed

+41
-20
lines changed

2 files changed

+41
-20
lines changed

docs/pages/api-reference/exceptions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424

2525
``` typescript
2626
class SignatureValidationFailed extends Error {
27-
public signature: string;
27+
public signature?: string;
2828
}
2929

3030
class JSONParseError extends Error {

docs/pages/guide/webhook.md

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ const config = {
6565
channelSecret: 'YOUR_CHANNEL_SECRET'
6666
}
6767

68-
app.use(middleware(config))
69-
70-
app.post('/webhook', (req, res) => {
68+
app.post('/webhook', middleware(config), (req, res) => {
7169
res.json(req.body.events) // req.body will be webhook event object
7270
})
7371

@@ -76,32 +74,53 @@ app.listen(8080)
7674

7775
We have imported `middleware` from the package and make the Express app to use
7876
the middleware. The middlware validates the request and parses webhook event
79-
object. It embeds body-parser and parses them to objects. Please keep in mind
80-
that it will not process requests without `X-Line-Signature` header. If you have
81-
a reason to use body-parser for other routes, *please do not use it before the
82-
LINE middleware*. body-parser parses the request body up and the LINE middleware
83-
cannot parse it afterwards.
77+
object. It embeds body-parser and parses them to objects. If you have a reason
78+
to use another body-parser separately for other routes, please keep in mind the
79+
followings.
80+
81+
### Do not use the webhook `middleware()` for other usual routes
8482

8583
``` js
8684
// don't
87-
app.use(bodyParser.json())
8885
app.use(middleware(config))
8986

9087
// do
91-
app.use(middleware(config))
88+
app.use('/webhook', middleware(config))
89+
```
90+
91+
The middleware will throw an exception when the [X-Line-Signature](https://devdocs.line.me/en/#signature-validation)
92+
header is not set. If you want to handle usual user requests, the middleware
93+
shouldn't be used for them.
94+
95+
### Do not use another body-parser before the webhook `middleware()`
96+
97+
``` js
98+
// don't
99+
app.use(bodyParser.json())
100+
app.use('/webhook', middleware(config))
101+
102+
// do
103+
app.use('/webhook', middleware(config))
92104
app.use(bodyParser.json())
93105
```
94106

95-
There are environments where `req.body` is pre-parsed, such as [Firebase Cloud Functions](https://firebase.google.com/docs/functions/http-events).
96-
If it parses the body into string or buffer, do not worry as the middleware will
97-
work just fine. If the pre-parsed body is an object, please use [`validateSignature()`](../api-reference/validate-signature.md)
98-
manually with the raw body.
107+
If another body parser already parsed a request's body, the webhook middleware
108+
cannot access to the raw body of the request. The raw body should be retrieved
109+
for signature validation.
110+
111+
However, there are environments where `req.body` is pre-parsed, such as
112+
[Firebase Cloud Functions](https://firebase.google.com/docs/functions/http-events).
113+
If it parses the body into string or buffer, the middleware will use the body
114+
as it is and work just fine. If the pre-parsed body is an object, the webhook
115+
middleware will fail to work. In the case, please use [`validateSignature()`](../api-reference/validate-signature.md)
116+
manually with raw body.
99117

100118
## Error handling
101119

102120
There are two types of errors thrown by the middleware, one is `SignatureValidationFailed`
103121
and the other is `JSONParseError`.
104122

123+
- `SignatureValidationFailed` is thrown when a request doesn't have a signature.
105124
- `SignatureValidationFailed` is thrown when a request has a wrong signature.
106125
- `JSONParseError` occurs when a request body cannot be parsed as JSON.
107126

@@ -130,13 +149,13 @@ app.post('/webhook', (req, res) => {
130149

131150
app.use((err, req, res, next) => {
132151
if (err instanceof SignatureValidationFailed) {
133-
res.status(401).send(err.signature);
134-
return;
152+
res.status(401).send(err.signature)
153+
return
135154
} else if (err instanceof JSONParseError) {
136-
res.status(400).send(err.raw);
137-
return;
155+
res.status(400).send(err.raw)
156+
return
138157
}
139-
next(err); // will throw default 500
158+
next(err) // will throw default 500
140159
})
141160

142161
app.listen(8080)
@@ -149,3 +168,5 @@ HTTPS server. For example, here is a [documentation](https://expressjs.com/en/ap
149168
of making Express work with HTTPS. You can also set HTTPS in web servers like
150169
[NGINX](https://www.nginx.com/). This guide will not cover HTTPS configuration,
151170
but do not forget to set HTTPS beforehand.
171+
172+
For development and test usages, [ngrok](https://ngrok.com/) works perfectly.

0 commit comments

Comments
 (0)