我想根据路由更改静态路径。例如(不工作):
const app = express();
const appRouter = express.Router();
const adminRouter = express.Router();
appRouter.use(express.static('/path/to/app/static/assets');
adminRouter.use(express.static('/path/to/admin/static/assets');
app.use('/', appRouter);
app.use('/admin', adminRouter);
这也行不通:
const app = express();
app.use('/', express.static('/path/to/app/static/assets');
app.use('/admin', express.static('/path/to/admin/static/assets');
我不想做的是将整个应用的两个路径都设置为静态:
// the following will expose both paths as static for the entire app
// this does not accomplish what I am trying to do
const app = express();
app.use(express.static('/path/to/app/static/assets');
app.use(express.static('/path/to/admin/static/assets');
这可能吗?
最佳答案
您尝试使用 express.static()
的方法无法实现您想要实现的目标。您的 #2 方法确实为 express.static 函数提供的文件创建了虚拟路径前缀(路径实际上并不存在于文件系统中)。关注this了解更多信息。
但似乎可以做的是在运行时改变 express.static()
的路径。请关注 this git issue .希望对您有所帮助。
关于javascript - 如何根据路由更改快速静态路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44148293/