CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/project/servers/browser/static.ts
Views: 687
1
import { Application, static as staticServer } from "express";
2
import index from "serve-index";
3
import { getLogger } from "@cocalc/project/logger";
4
5
export default function init(app: Application, base: string) {
6
const winston = getLogger("serve-static-files-to-browser");
7
winston.info(`initialize with base="${base}"`);
8
// Setup the static raw HTTP server. This must happen after anything above,
9
// since it serves all URL's (so it has to be the fallback).
10
app.use(base, (req, res, next) => {
11
// this middleware function has to come before the express.static server!
12
// it sets the content type to octet-stream (aka "download me") if URL query ?download exists
13
if (req.query.download != null) {
14
res.setHeader("Content-Type", "application/octet-stream");
15
}
16
// Note: we do not set no-cache since that causes major issues on Safari:
17
// https://github.com/sagemathinc/cocalc/issues/5120
18
// By now our applications should use query params to break the cache.
19
res.setHeader("Cache-Control", "private, must-revalidate");
20
next();
21
});
22
23
const { HOME } = process.env;
24
if (HOME == null) {
25
throw Error("HOME env variable must be defined");
26
}
27
winston.info(`serving up HOME="${HOME}"`);
28
29
app.use(base, index(HOME, { hidden: true, icons: true }));
30
app.use(base, staticServer(HOME, { dotfiles: "allow" }));
31
}
32
33