@@ -65,9 +65,7 @@ const config = {
65
65
channelSecret: ' YOUR_CHANNEL_SECRET'
66
66
}
67
67
68
- app .use (middleware (config))
69
-
70
- app .post (' /webhook' , (req , res ) => {
68
+ app .post (' /webhook' , middleware (config), (req , res ) => {
71
69
res .json (req .body .events ) // req.body will be webhook event object
72
70
})
73
71
@@ -76,32 +74,53 @@ app.listen(8080)
76
74
77
75
We have imported ` middleware ` from the package and make the Express app to use
78
76
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
84
82
85
83
``` js
86
84
// don't
87
- app .use (bodyParser .json ())
88
85
app .use (middleware (config))
89
86
90
87
// 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))
92
104
app .use (bodyParser .json ())
93
105
```
94
106
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.
99
117
100
118
## Error handling
101
119
102
120
There are two types of errors thrown by the middleware, one is ` SignatureValidationFailed `
103
121
and the other is ` JSONParseError ` .
104
122
123
+ - ` SignatureValidationFailed ` is thrown when a request doesn't have a signature.
105
124
- ` SignatureValidationFailed ` is thrown when a request has a wrong signature.
106
125
- ` JSONParseError ` occurs when a request body cannot be parsed as JSON.
107
126
@@ -130,13 +149,13 @@ app.post('/webhook', (req, res) => {
130
149
131
150
app .use ((err , req , res , next ) => {
132
151
if (err instanceof SignatureValidationFailed) {
133
- res .status (401 ).send (err .signature );
134
- return ;
152
+ res .status (401 ).send (err .signature )
153
+ return
135
154
} else if (err instanceof JSONParseError) {
136
- res .status (400 ).send (err .raw );
137
- return ;
155
+ res .status (400 ).send (err .raw )
156
+ return
138
157
}
139
- next (err); // will throw default 500
158
+ next (err) // will throw default 500
140
159
})
141
160
142
161
app .listen (8080 )
@@ -149,3 +168,5 @@ HTTPS server. For example, here is a [documentation](https://expressjs.com/en/ap
149
168
of making Express work with HTTPS. You can also set HTTPS in web servers like
150
169
[ NGINX] ( https://www.nginx.com/ ) . This guide will not cover HTTPS configuration,
151
170
but do not forget to set HTTPS beforehand.
171
+
172
+ For development and test usages, [ ngrok] ( https://ngrok.com/ ) works perfectly.
0 commit comments