简体   繁体   English

如何关闭app.use(express.static('/ public')); 快递js

[英]How to turn off app.use(express.static('/public')); Express js

I am using Express js. 我正在使用Express js。 In the root folder, I have a public folder which contains subfolders stylesheets , javascripts , fonts , images and html . 在根文件夹中,我有一个public其中包含子文件夹stylesheetsjavascriptsfontsimageshtml I want to protect my assets if the user is not logged in. For example, if someone opens the web browser and without login make a request for http://localhost:3000/images/hello.jpg then the request should be redirected to login page. 如果用户未登录,我想保护我的资产。例如,如果某人打开Web浏览器但未登录,则请求http://localhost:3000/images/hello.jpg则该请求应重定向到登录页面。

To achieve this I am using express.static(path.join(__dirname, '/public')) conditionally. 为此,我express.static(path.join(__dirname, '/public'))使用express.static(path.join(__dirname, '/public'))

Challenge 1 挑战1

Login page also having some 4 to 5 assets (CSS and JS files), which are placed in the same public folder. 登录页面还具有大约4到5个资产(CSS和JS文件),它们位于同一公用文件夹中。

Challenge 2 挑战2

I am using this code express.static(path.join(__dirname, '/public')) like this: 我正在像这样使用这段代码express.static(path.join(__dirname, '/public'))

if(userIsLoggedIn){
   express.static(path.join(__dirname, '/public'))
}else{
   //what to do here???
   //I need a code here which makes public folder as non-static folder
}

For example, we have 2 users A and B. Both users are using different computers. 例如,我们有两个用户A和B。两个用户都使用不同的计算机。 User A is doing successful login and at the server, we are setting the public folder as static but user B has not done login he is now directly accessing the assets and he is able to access. 用户A是做成功登录,并在服务器上,我们设置的public文件夹static但用户B没有做过登录他现在直接访问的资产,他是能够访问。

This is happening because we are not setting the public folder as static on user basis we are making it static if any of user do a successful login. 之所以发生这种情况,是因为我们没有将基于用户的公用文件夹设置为静态,如果有任何用户成功登录,则将其设为静态。

you can add an extra middleware before the express.static to force a user validation. 您可以在express.static之前添加一个额外的中间件,以强制用户验证。

var VerifyUser = require('./verifyuser');
app.use(VerifyUser, express.static(__dirname + '/public'));


//VerifyUser code
module.exports = function(req, res, next) {
    if(user.isLogged) 
      next();
    else
     next(err); //presume you have some error handler middleware
};

A quick way would be to simply mkdir an empty directory and point to it. 一种快速的方法是简单地mkdir一个空目录并指向它。 Or add files to it that you don't mind having public. 或向其中添加您不希望公开的文件。

express.static(path.join(__dirname, '/someemptydirectory'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 app.use(express.static('public')) 时如何测试 Express 应用程序; - How to test an Express application when using app.use(express.static('public')); 如何配置express.js的app.use(express.static(…)`? - How to configure express.js's `app.use(express.static(…)`? app.use(“ /”,express.static)和app.use(express.static)之间有区别吗? - Is there a difference between app.use(“/”, express.static) and app.use(express.static)? 我的 javascript 文件没有链接? 我正在使用: app.use(express.static(__dirname + '/public')); - my javascript file not linking? Iam using: app.use(express.static(__dirname + '/public')); 如果app-是子应用程序,则`app.use(express.static`似乎不起作用 - `app.use(express.static` seems to not working if app - is subapplication app.use(express.static("public")) 是否为每个请求调用中间件? - Does app.use(express.static("public")) call the middleware for every request? 与 app.use(express.static(...)) 相关的 next() 中间件/路由是什么? - What are the next() middlewares/routes relative to app.use(express.static(…))? 如何通过express.static将静态CSS添加到Node JS应用 - How to add static css to node js app by express.static app.use(express.raw()) 在 Express.js 中不起作用 - app.use(express.raw()) not working in Express.js 如何在app.use express中配置强大? - how to configure formidable in app.use express?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM