Skip to content

Commit 9ec7775

Browse files
committed
fix robot.txt to ./api
1 parent f4d284a commit 9ec7775

File tree

4 files changed

+21
-19
lines changed

4 files changed

+21
-19
lines changed

api/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
"drizzle-orm": "^0.33.0",
2323
"express": "^4.21.0",
2424
"express-rate-limit": "^7.4.0",
25-
"express-robots-txt": "^1.0.0",
2625
"fs-extra": "^11.2.0",
2726
"helmet": "^7.1.0",
2827
"lodash": "^4.17.21",

api/src/app/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import Container from "typedi";
2020

2121
import { ErrorMiddleware } from "./middlewares/error";
2222
import { LoggerMiddleware } from "./middlewares/logger";
23-
import { RobotsMiddleware } from "./middlewares/robots";
23+
import { RobotsController } from "./middlewares/robots";
2424
import { SecurityMiddleware } from "./middlewares/security";
2525

2626
// Use typedi container
@@ -43,8 +43,9 @@ const routingControllersOptions: RoutingControllersOptions = {
4343
MilestoneController,
4444
ProjectController,
4545
ContributorController,
46+
RobotsController,
4647
],
47-
middlewares: [SecurityMiddleware, ErrorMiddleware, LoggerMiddleware, RobotsMiddleware],
48+
middlewares: [SecurityMiddleware, ErrorMiddleware, LoggerMiddleware],
4849
defaultErrorHandler: false,
4950
cors: Container.get(SecurityMiddleware).cors(),
5051
};

api/src/app/middlewares/robots.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1-
import { RequestHandler } from "express";
2-
import { ExpressMiddlewareInterface, Middleware } from "routing-controllers";
1+
import { ConfigService } from "src/config/service";
32
import { Service } from "typedi";
4-
const robots = require("express-robots-txt"); // eslint-disable-line @typescript-eslint/no-require-imports
3+
import { Response } from "express";
4+
import { Controller, Res, Get } from "routing-controllers";
55

66
@Service()
7-
@Middleware({ type: "before", priority: 0 })
8-
export class RobotsMiddleware implements ExpressMiddlewareInterface {
9-
use: RequestHandler = robots({ UserAgent: "*", Disallow: "/docs" });
7+
@Controller("/robots.txt")
8+
export class RobotsController {
9+
private readonly stage = this.configService.env().NODE_ENV;
10+
11+
constructor(private readonly configService: ConfigService) {}
12+
13+
@Get("/")
14+
public async robotsTxt(@Res() response: Response): Promise<Response> {
15+
response.set("Content-Type", "text/plain");
16+
if (this.stage === "production") {
17+
return response.send("User-agent: *\nAllow: /");
18+
} else {
19+
return response.send("User-agent: *\nDisallow: /");
20+
}
21+
}
1022
}

package-lock.json

Lines changed: 0 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)