Skip to content

Commit

Permalink
实现限制访问静态文件
Browse files Browse the repository at this point in the history
  • Loading branch information
hauk0101 committed Aug 23, 2017
1 parent d785a2a commit d139805
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
43 changes: 29 additions & 14 deletions app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,49 @@ app.set('view engine', 'ejs');
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());
//静态资源访问时的跨域设置
app.all('*', function(req, res, next) {
app.all('*', function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
next();
});
//静态资源访问限制,
app.use(function (req, res, next) {
var static = /^(\/public|\/key)/g;//设置指定文件目录
var suffix = /(\.key)$/g;//后缀格式指定
console.log(!req.session);
// if (((req.session.username != 'admin') || (req.session.password != 'admin'))
// && (static.test(req.path) && suffix.test(req.path) )) {
// console.log('用户未登录,不允许访问key文件');
// return res.send('请求非法');
// }
// else {
// console.log('当前用户已登录');
next();
// }
});
app.use(express.static(path.join(__dirname, 'public')));

//路由
app.use('/', router(app));

// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
app.use(function (req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});

// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};

// render the error page
res.status(err.status || 500);
res.render('error');
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};

// render the error page
res.status(err.status || 500);
res.render('error');
});

module.exports = app;
2 changes: 2 additions & 0 deletions app/routes/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ router.get('/login', function (req, res) {
//权限登录处理
router.post('/login',function(req,res){
console.log("登录信息:",req.body);
req.session.username = req.body.username;
req.session.password = req.body.password;
});
//视频播放页面
router.get('/player', function (req, res) {
Expand Down

0 comments on commit d139805

Please sign in to comment.