MCPcopy
hub / github.com/QwikDev/qwik / staticFile

Function staticFile

packages/qwik-city/src/middleware/node/index.ts:96–138  ·  view source on GitHub ↗
(
    req: IncomingMessage | Http2ServerRequest,
    res: ServerResponse,
    next: (e?: any) => void
  )

Source from the content-addressed store, hash-verified

94 };
95
96 const staticFile = async (
97 req: IncomingMessage | Http2ServerRequest,
98 res: ServerResponse,
99 next: (e?: any) => void
100 ) => {
101 try {
102 const origin = computeOrigin(req, opts);
103 const url = getUrl(req, origin);
104 if (isStaticPath(req.method || 'GET', url)) {
105 const pathname = url.pathname;
106 let filePath: string;
107 if (basename(pathname).includes('.')) {
108 filePath = join(staticFolder, pathname);
109 } else if (opts.qwikCityPlan.trailingSlash) {
110 filePath = join(staticFolder, pathname + 'index.html');
111 } else {
112 filePath = join(staticFolder, pathname, 'index.html');
113 }
114 const ext = extname(filePath).replace(/^\./, '');
115 const stream = createReadStream(filePath);
116 stream.on('error', next);
117
118 const contentType = MIME_TYPES[ext];
119
120 if (contentType) {
121 res.setHeader('Content-Type', contentType);
122 }
123
124 if (opts.static?.cacheControl) {
125 res.setHeader('Cache-Control', opts.static.cacheControl);
126 }
127
128 stream.pipe(res);
129
130 return;
131 }
132
133 return next();
134 } catch (e) {
135 console.error(e);
136 next(e);
137 }
138 };
139
140 return {
141 router,

Callers

nothing calls this directly

Calls 8

computeOriginFunction · 0.90
getUrlFunction · 0.90
isStaticPathFunction · 0.85
joinFunction · 0.85
extnameFunction · 0.85
onMethod · 0.80
basenameFunction · 0.50
nextFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…