Express - mongoose-auth 路由问题
我有以下咖啡脚本代码。使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有将 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 usingapp.use
, which is fine, but you need to pass the middleware function toapp.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.