在Express中有条件提供静态文件的最佳方法是什么? [英] What is the best way to conditionally serve static files in Express?

查看:123
本文介绍了在Express中有条件提供静态文件的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个包含两个独立部分的应用程序,在前端,我将其构建为两个独立的Angular应用程序.我这样做是为了更好地将控件访问权限划分给他的代码库,而不是不必要地使某些团队成员访问他们不需要的代码.

I'm building an application that has two separate parts to it, which on the frontend I am building as two separate Angular apps. I am doing it this way so that I can better divide control access to he codebase and not unnecessarily give some team members access to code they do not need.

因此,有两个单独的应用程序,它们由同一台NodeJS服务器提供服务.服务的应用程序取决于用户是否登录.如果他们是访客用户,则将获得一个版本的应用程序;如果他们是注册用户,则将获得具有更多功能的特权版本.

So there are two separate applications, served from the same NodeJS server. The app that is served depends on whether the user is logged in. If they are a guest user, they get one version app, if they are registered user they get a privileged version of the app with more features.

如何在Express中有条件/动态地提供静态文件,以便说:如果User1是来宾,则提供Application A,否则提供Application B"?

How can I conditionally/dynamically serve static files in Express, so as to say, "if User1 is a guest, serve Application A, otherwise serve Application B"?

推荐答案

如果您只是在谈论要提供的文件(例如app-1.jsapp-2.js),则不需要express.static.您可以使用res.sendFile在常规路由处理程序中处理请求,如下所示:

If it's just one file you're talking about serving (e.g. app-1.js or app-2.js) then you don't need express.static. You can just handle the request in a normal route handler with res.sendFile like so:

app.get('/get-app', function(req, res) {
  if (userIsLoggedIn()) {
    res.sendFile('/path-to-logged-in-app.js')
  } else {
    res.sendFile('/path-to-logged-out-app.js')
  }
})

有关res.sendFile的更多信息此处.

如果要通过express.static提供多个文件,则需要两个express.static中间件实例,并且在其上需要另一个中间件实例,以根据用户的登录状态来修改请求url.也许遵循以下内容:

If you want to serve multiple files via express.static, you'll need two instances of express.static middleware, and another piece of middleware on top of that to modify the request url depending up on the user's logged in status. Maybe something along the lines of the following:

// Middleware function to modify the request url based on user's status
function checkUser(req, res, next) { 
 if (userIsLoggedIn()) {
    req.url = `/app-1/${req.url}`
  } else {
    req.url = `/app-2/${req.url}`
  }

  next()
}

// Set up the middleware stack
app.use(checkUser)
app.use('/app-1', express.static(path.join(__dirname, 'app-1')))
app.use('/app-2', express.static(path.join(__dirname, 'app-2')))

有关在此处编写自己的中间件的更多信息.您可能还想在checkUser中添加一些逻辑,以仅在请求静态文件的url之前添加前缀(例如,向/assets/foo.js的请求url获得前缀处理,而对/api/some-api-function的请求则不添加前缀).

More about writing your own middleware here. You also might want to add some logic in checkUser to only prepend to urls requesting static files (e.g. request urls to /assets/foo.js get the prepend treatment but requests to /api/some-api-function do not).

所有这些,我对Angular不太熟悉,但我建议您研究其他方法来提供已登录/已退出内容的服务.我假设在Angular中有一些组件或视图"的概念,只渲染"LoggedIn"或"LoggedOut"组件/视图与发送一个完全不同的"app"相比可能要干净得多.

All that said, I'm not that familiar with Angular but I'd suggest investigating other ways to serve logged in / logged out content. I'm assuming there is some concept of components or "views" in Angular, and it's probably a lot cleaner to just render a "LoggedIn" or "LoggedOut" component/view vs. sending a whole different "app".

这篇关于在Express中有条件提供静态文件的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆