首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >(../../portal/node_modules/send/index.js:270:31):ForbiddenError:禁止在SendStream.error

(../../portal/node_modules/send/index.js:270:31):ForbiddenError:禁止在SendStream.error
EN

Stack Overflow用户
提问于 2018-04-10 12:18:15
回答 2查看 2.1K关注 0票数 1

这是之前的工作,但后来我把/server.js搬到了/server/controllers/oauth.controller.js

更早的时候,我可以使用Github登录到这个网站。

访问127.0.0.1:4568时错误

代码语言:javascript
复制
>node oauth.controller.js                                 ✗ authentication (origin/authentication)
server is listening on 4568
ForbiddenError: Forbidden
    at SendStream.error (/Users/abhimanyuaryan/portal/node_modules/send/index.js:270:31)
    at SendStream.pipe (/Users/abhimanyuaryan/portal/node_modules/send/index.js:554:12)
    at sendfile (/Users/abhimanyuaryan/portal/node_modules/express/lib/response.js:1099:8)
    at ServerResponse.sendFile (/Users/abhimanyuaryan/portal/node_modules/express/lib/response.js:429:3)
    at app.get (/Users/abhimanyuaryan/portal/server/controllers/oauth.controller.js:72:7)
    at Layer.handle [as handle_request] (/Users/abhimanyuaryan/portal/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/abhimanyuaryan/portal/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/Users/abhimanyuaryan/portal/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/Users/abhimanyuaryan/portal/node_modules/express/lib/router/layer.js:95:5)
    at /Users/abhimanyuaryan/portal/node_modules/express/lib/router/index.js:281:22

/server/controllers/oauth.controllers.js

代码语言:javascript
复制
let Express = require('express')
let bodyParser = require('body-parser')
let session = require('express-session')
let passport = require('passport')
let GithubStrategy = require('passport-github2').Strategy

let GITHUB_CLIENT_ID = "xxxxxxxxxxxxxxxxx"
let GITHUB_CLIENT_SECRET = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

// flow #4
passport.serializeUser((user, done) => {
    done(null, user);
})

// flow #6
passport.deserializeUser((user, done) => {
    done(null, user);
})

passport.use(new GithubStrategy({
    clientID: GITHUB_CLIENT_ID,
    clientSecret: GITHUB_CLIENT_SECRET,
    callbackURL: "http://127.0.0.1:4568/auth/github/callback"
  },
  (accessToken, refreshToken,  profile, done) => {
    // console.log(profile)
    /*
    flow #3
    Profile is the json result from github, it contains helpful information like id, username, email etc.
    You can decide to use profile.id as your internal userId too.
    Here you can call your database and check if the user already exist and create a new record if it doesn't 
    exists. We are not going to include this logic here to keep things simple but you can manage
    the profile data if whatever way you want
    */
    // for simplicity we are only going to return the whole profile
    return done(null, profile)
  }
))

let app = Express()
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: true}))
app.use(Express.static(__dirname + '../../client', {dotfiles:'allow'}))
app.use(session({
    secret: 'top secret key',
    resave: false,
    saveUninitialized: true
}))
app.use(passport.initialize())
app.use(passport.session())

// Explorting modules to external file
var exports = module.exports = {};

// function that will check if the user is authenticated
exports.isAuthenticated = (req, res, next) => {
    if(req.isAuthenticated()){
        return next()
    }
    res.redirect('/login')
}

// you can put 'isAuthenticated function in any get/post call, here is an example'
app.get('/', exports.isAuthenticated,
    (req, res) => {
        res.sendFile(__dirname + '../../client/secret.html')
    }
)

app.get('/login',
    (req, res) => {
        res.sendFile(__dirname + '../../client/login.html')
    }
)

app.get('/logout', 
    (req, res) => {
        req.logout()
        res.sendFile(__dirname + '../../client/login.html')
    }
)

// 'Sign in with Github' link click will arrive here and from here we call Github API with passport. authenticate
app.get('/auth/github',
    //flow #1
    passport.authenticate('github', {scope: [ 'user:email']}),
    (req, res) => {
    }
)

//github responses will arrive here and if its failure we will to /login
// if its successful we will redirect to ('/')
app.get('/auth/github/callback',
    // flow #2
    passport.authenticate('github', {failureRedirect: '/login'}),
    (req, res) => {
        // flow #5
        res.redirect('/')
    }
)

console.log('server is listening on 4568')
app.listen(4568)

直接君主国

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-04-11 10:21:40

引用这个链接的话。

该错误来自于包含..。(向上的父目录)并且没有提供根选项。尝试使用sendfile如下所示:

代码语言:javascript
复制
res.sendfile(path, {'root': '/path/to/root/directory'});

根选项应该是要为文件提供服务的目录。它的目的是防止路径包含这样的东西。因此,用户可以让服务器在该目录之外提供一个文件。

票数 3
EN

Stack Overflow用户

发布于 2022-07-08 18:33:23

对于其他像我这样的新手来说,OP需要改变。

代码语言:javascript
复制
res.sendFile(__dirname + '../../client/secret.html');

在他们的代码中

代码语言:javascript
复制
res.sendFile('secret.html', {'root': __dirname + '/../../client/'});

感谢abhimanyuaryan和github.com/dougwilson让我走上了正确的道路。还需要一段时间才能弄清楚。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49753464

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档