MCPcopy Index your code
hub / github.com/11ty/dev-server / mapUrlToFilePath

Method mapUrlToFilePath

server.js:269–342  ·  view source on GitHub ↗
(url)

Source from the content-addressed store, hash-verified

267 * /resource/ matches /resource/index.html
268 */
269 mapUrlToFilePath(url) {
270 // Note: `localhost` is not important here, any host would work
271 let u = new URL(url, "http://localhost/");
272 url = u.pathname;
273
274 // Remove PathPrefix from start of URL
275 if (this.options.pathPrefix !== "/") {
276 // Requests to root should redirect to new pathPrefix
277 if(url === "/") {
278 return {
279 statusCode: 302,
280 url: this.options.pathPrefix,
281 }
282 }
283
284 // Requests to anything outside of root should fail with 404
285 if (!url.startsWith(this.options.pathPrefix)) {
286 return {
287 statusCode: 404,
288 };
289 }
290
291 url = url.slice(this.options.pathPrefix.length - 1);
292 }
293
294 let rawPath = this.getOutputDirFilePath(url);
295 if (this.isOutputFilePathExists(rawPath)) {
296 return {
297 statusCode: 200,
298 filepath: rawPath,
299 };
300 }
301
302 let indexHtmlPath = this.getOutputDirFilePath(url, this.options.indexFileName);
303 let indexHtmlExists = fs.existsSync(indexHtmlPath);
304
305 let htmlPath = this.getOutputDirFilePath(url, ".html");
306 let htmlExists = fs.existsSync(htmlPath);
307
308 // /resource/ => /resource/index.html
309 if (indexHtmlExists && url.endsWith("/")) {
310 return {
311 statusCode: 200,
312 filepath: indexHtmlPath,
313 };
314 }
315 // /resource => resource.html
316 if (htmlExists && !url.endsWith("/")) {
317 return {
318 statusCode: 200,
319 filepath: htmlPath,
320 };
321 }
322
323 // /resource => redirect to /resource/
324 if (indexHtmlExists && !url.endsWith("/")) {
325 return {
326 statusCode: 301,

Callers 2

testServer.jsFile · 0.80

Calls 2

getOutputDirFilePathMethod · 0.95

Tested by

no test coverage detected