-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
51 lines (45 loc) · 1.23 KB
/
app.js
File metadata and controls
51 lines (45 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import "express-async-error";
import fs from "fs";
import express from "express";
import {
NotFound,
rateLimiter,
ErrorHandler,
securityMiddleware,
} from "./middlewares/index.js";
import { configDotenv } from "dotenv";
import connectDb from "./db/connect.js";
import SwaggerUI from "swagger-ui-express";
import stringRouter from "./routes/string.js";
configDotenv();
const app = express();
const swaggerDocument = JSON.parse(fs.readFileSync("./swagger.json", "utf-8"));
// security middleware
securityMiddleware(app);
// swagger docs
app.use("/api-docs", SwaggerUI.serve, SwaggerUI.setup(swaggerDocument));
// api rate limit
app.use("/api/v1", rateLimiter);
// routes
app.get("/", (_, res) =>
/* #swagger.tags = ['Home'] */
res.send(
'<h1>String Analyzer API</h1><a href="/api-docs">Swagger Documentation</a>'
)
);
app.use("/strings", /* #swagger.tags = ['Strings'] */ stringRouter);
// error handler
app.use(NotFound);
app.use(ErrorHandler);
// self-invoked function
(async () => {
const port = process.env.PORT || 3000;
try {
await connectDb();
app.listen(port, () => {
console.log("Server running at port:", port);
});
} catch (error) {
console.log("Error while running app:", error.message);
}
})();