-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
57 lines (49 loc) · 1.32 KB
/
server.js
File metadata and controls
57 lines (49 loc) · 1.32 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
52
53
54
55
56
57
import express from "express";
import "dotenv/config";
import fileUpload from "express-fileupload";
import helmet from "helmet";
import cors from "cors";
import cookieParser from "cookie-parser";
import morgan from "morgan";
import { limiter } from "./config/ratelimit.config.js";
const app = express();
const PORT = process.env.PORT || 8000;
// * Middlewares
app.use(morgan("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(helmet());
app.use(cors());
app.use(express.static("public")); //statically serve files from 'public' directory
app.use(fileUpload());
app.use(limiter);
app.get("/", (req, res) => {
return res.status(200).json({
success: true,
message: "Hi, welcome here.. ",
});
});
// * Import routes
import { apiRouter } from "./routes/api.js";
app.use("/api", apiRouter);
// * 404 route for unmatched paths
app.use((req, res) => {
res.status(404).json({
success: false,
message: "Route not found.",
});
});
// * general error handling middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({
success: false,
message: "An unexpected error occurred.",
});
});
// * jobs import
import "./jobs/index.js";
app.listen(PORT, () => {
console.log(`Server is running on port : ${PORT}`);
});