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

Method eleventyProjectMiddleware

server.js:560–624  ·  view source on GitHub ↗
(req, res)

Source from the content-addressed store, hash-verified

558
559 // This runs at the end of the middleware chain
560 eleventyProjectMiddleware(req, res) {
561 // Known issue with `finalhandler` and HTTP/2:
562 // UnsupportedWarning: Status message is not supported by HTTP/2 (RFC7540 8.1.2.4)
563 // https://github.com/pillarjs/finalhandler/pull/34
564
565 let lastNext = finalhandler(req, res, {
566 onerror: (e) => {
567 if (e.statusCode === 404) {
568 let localPath = TemplatePath.stripLeadingSubPath(
569 e.path,
570 TemplatePath.absolutePath(this.dir)
571 );
572 this.logger.error(
573 `HTTP ${e.statusCode}: Template not found in output directory (${this.dir}): ${localPath}`
574 );
575 } else {
576 this.logger.error(`HTTP ${e.statusCode}: ${e.message}`);
577 }
578 },
579 });
580
581 // middleware (maybe a serverless request) already set a body upstream, skip this part
582 if(!res._shouldForceEnd) {
583 let match = this.mapUrlToFilePath(req.url);
584 debug( req.url, match );
585
586 if (match) {
587 if (match.statusCode === 200 && match.filepath) {
588 // Content-Range request, probably Safari trying to stream video
589 if (req.headers.range) {
590 return send(req, match.filepath).pipe(res);
591 }
592
593 return this.renderFile(match.filepath, res);
594 }
595
596 // Redirects, usually for trailing slash to .html stuff
597 if (match.url) {
598 res.statusCode = match.statusCode;
599 res.setHeader("Location", match.url);
600 return res.end();
601 }
602
603 let raw404Path = this.getOutputDirFilePath("404.html");
604 if(match.statusCode === 404 && this.isOutputFilePathExists(raw404Path)) {
605 res.statusCode = match.statusCode;
606 res.isCustomErrorPage = true;
607 return this.renderFile(raw404Path, res);
608 }
609 }
610 }
611
612 if(res.body && !res.bodyUsed) {
613 if(res._shouldForceEnd) {
614 res.end();
615 } else {
616 let err = new Error("A response was never written to the stream. Are you missing a server middleware with `res.end()`?");
617 err.statusCode = 500;

Callers

nothing calls this directly

Calls 5

mapUrlToFilePathMethod · 0.95
renderFileMethod · 0.95
getOutputDirFilePathMethod · 0.95
errorMethod · 0.80

Tested by

no test coverage detected