1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

@line/bot-sdkとbody-parserの競合回避

Posted at

#LINEボットむずい
LINE-botはこちらを参考に製作中ですが,
LINE以外からPOSTを受け取り処理をしたいのに
@line/bot-sdkとbody-parserが競合するため悩んでいたところ,
こちらのページからヒントを得て解決したのでメモ.

#解決策
body-parserを使いたいPOST処理だけapp.use(bodyParser.json())する

##実装例
いろいろ省略してます

index.js
const line = require('@line/bot-sdk');
const bp = require('body-parser');

app.use('/else',bp.json());

//webhookにはbody-parserが効かない
app.post('/webhook', line.middleware(config), (req, res) => {
  Promise
    .all(req.body.events.map(handleEvent))
    .then((result) => res.json(result));
});

//parseできる
app.post('/else', function(req, res) {
  user_input = req.body.message;
  res.json(user_input);
}

もっと賢いやり方あれば教えてください.

1
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?