首页 > 解决方案 > 将express与ejs一起使用时如何从磁盘或内存中加载资源

问题描述

我正在使用Firebase托管通过ExpressEjs提供我的网站内容。

每次用户请求模板页面时,如何从磁盘或内存加载资源(CSS、Javascript、图像、字体)而不是请求资源?

以其他格式“如何将状态代码从 304 更改为 200(从磁盘缓存)? ”。

在此处输入图像描述

索引.ejs

<!DOCTYPE html>
<html>
  <head>
    <title><%= PageTitle %></title>
    <link rel='stylesheet' href='assets/css/reset.css'>
    <link rel='stylesheet' href='assets/css/icons.css'>
    <link rel='stylesheet' href='assets/css/plyr.css'>
    <link rel='stylesheet' href='assets/css/styles.css'>
  </head>
  <body>
    <h1><%= PageTitle %></h1>
    <script src='assets/js/jquery.min.js'/>
    <script src='assets/js/plyr.js'/>
    <script src='assets/js/scripts.js'/>
  </body>
</html>

index.js

const express = require('express');
const ejs = require('ejs');
const app = express();

app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public'));

app.get('/', (req, res) => {  
  res.render('index', { PageTitle: "Homepage" });
});

exports.app = functions.https.onRequest(app);

firebase.json

{
  "hosting": {
    "public": "functions/views",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "**",
        "function": "app"
      }
    ]
  }
}

标签: node.jsfirebaseexpressgoogle-cloud-functionsejs

解决方案


推荐阅读