Express - mongoose-auth 路由问题

发布于 2024-11-19 11:44:47 字数 1732 浏览 1 评论 0原文

我有以下咖啡脚本代码。使用express和mongoose-auth,根据文档,使用中间件应该可以启用路由删除。这意味着在我调用 /auth/twitter 之后我应该使用 twitter 登录。不幸的是,由于某种原因我收到了 404 错误。有什么想法吗?

提前致谢。

express       = require 'express'
mongoose      = require 'mongoose'
mongoose_auth = require 'mongoose-auth'

UserSchema = new mongoose.Schema {}
User

UserSchema.plugin mongoose_auth, {
  everymodule: {
    everyauth: {
      User: () ->
        return User
    }
  }
  twitter: {
    everyauth: {
      myHostname:     'http://localhost:3000'
      consumerKey:    ''
      consumerSecret: ''
      redirectPath:   '/'
    }
  }
}

mongoose.model 'User', UserSchema

mongoose.connect 'mongodb://localhost/foobar'

User = mongoose.model 'User'

app = module.exports = express.createServer()

app.configure ->
  app.set 'views', "#{__dirname}/views"
  app.set 'view engine', 'jade'
  app.use require('stylus').middleware { src: "#{__dirname}/public" }
  app.use express.static "#{__dirname}/public"
  app.use express.bodyParser()
  app.use express.methodOverride()
  #app.use app.router
  app.use express.cookieParser()
  app.use express.session { secret: 'foobarmoocowetc' }
  mongoose_auth.middleware()

app.configure 'development', () ->
  app.use express.errorHandler {
    dumpExceptions: true
    showStack: true
  }

app.configure 'production', () ->
  app.use express.errorHandler()

app.get '/', (req, res) ->
  res.render 'page/index'

mongoose_auth.helpExpress app

app.listen 3000
console.log 'Express server listening on port %d in %s mode', app.address().port, app.settings.env

I have the following coffescript code. Playing with express and mongoose-auth, according to the documentation, using the middleware should enable the drop in routing. Which means that after I call /auth/twitter I should login with twitter. Unfortunately for some reason I got 404 error. Any ideas?

Thanks in advance.

express       = require 'express'
mongoose      = require 'mongoose'
mongoose_auth = require 'mongoose-auth'

UserSchema = new mongoose.Schema {}
User

UserSchema.plugin mongoose_auth, {
  everymodule: {
    everyauth: {
      User: () ->
        return User
    }
  }
  twitter: {
    everyauth: {
      myHostname:     'http://localhost:3000'
      consumerKey:    ''
      consumerSecret: ''
      redirectPath:   '/'
    }
  }
}

mongoose.model 'User', UserSchema

mongoose.connect 'mongodb://localhost/foobar'

User = mongoose.model 'User'

app = module.exports = express.createServer()

app.configure ->
  app.set 'views', "#{__dirname}/views"
  app.set 'view engine', 'jade'
  app.use require('stylus').middleware { src: "#{__dirname}/public" }
  app.use express.static "#{__dirname}/public"
  app.use express.bodyParser()
  app.use express.methodOverride()
  #app.use app.router
  app.use express.cookieParser()
  app.use express.session { secret: 'foobarmoocowetc' }
  mongoose_auth.middleware()

app.configure 'development', () ->
  app.use express.errorHandler {
    dumpExceptions: true
    showStack: true
  }

app.configure 'production', () ->
  app.use express.errorHandler()

app.get '/', (req, res) ->
  res.render 'page/index'

mongoose_auth.helpExpress app

app.listen 3000
console.log 'Express server listening on port %d in %s mode', app.address().port, app.settings.env

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡水深流 2024-11-26 11:44:47

您没有将 mongoose_auth.middleware() 放入express的中间件列表中。您需要

app.use mongoose_auth.middleware()

他们的方式是,中间件刚刚创建并被忽略。可能您按照自述文件中的示例进行操作,但是当他调用 createServer 时,他正在设置中间件,并且您正在使用 app.use,这很好,但是您需要传递中间件函数 app.use 以便正确注册它。

这至少是一个问题。解决这个问题,看看会发生什么。有很多配置细节必须完美才能让 everyauth 正常工作,因此您可能还需要进行一些其他调整。

You aren't putting mongoose_auth.middleware() into express's middleware list. You need

app.use mongoose_auth.midleware()

They way you have it the middleware is just created and ignored. Probably you followed the example in the README but he's setting up his middleware when he calls createServer and you are using app.use, which is fine, but you need to pass the middleware function to app.use in order to properly register it.

That's one problem at least. Fix that and see what happens. There are a lot of configuration details that must be perfect to get everyauth working, so you may have a few other tweaks required as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文