LoginSignup
21

More than 5 years have passed since last update.

express + passport + mongoose + kue

Posted at

Node.jsで実践的なWebアプリ開発をやったメモ

ライブラリのドキュメントとリポジトリ

組み合わせてできること

  • passportとpassport-twitterでTwitter OAuth認証して、mongooseでUserモデルと連動させてトークンなどを保存、取り出し
  • mongooseから特定の条件でUserモデルを取り出してkueでジョブを登録して実行させる
    • 自動ツイートなどをジョブキューで実装

引っかかった点

  • passportのログイン時処理をmongooseのモデル内に書きたい

    • → staticメソッドとして定義してログイン時の引数をそのまま受け取るように実装すれば書ける、動く
  • mongooseのモデルをAPI経由でjsonで出力するときに、表に出したくないフィールドがある

    • → schema.set("toJSON", {transform: function(doc, ret, options){...} }) でjsonにするときの処理を定義できる
  • mongooseでpaginationをやりたい

    • → require "mongoose-pagination"
  • kueを扱うスクリプトでmongoose使う方法

    • require '../models/user'とかやって問題なく動く
    • modelの依存を少なくしておかないと外から呼ぶ時面倒になる可能性はある
    • 終了処理だけmongoose.connection.close()を呼ぶように気を付ける必要がある

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
21