42 lines
944 B
JavaScript
42 lines
944 B
JavaScript
import express from "express";
|
|
import cors from "cors";
|
|
import env from "dotenv";
|
|
env.config();
|
|
const app = express();
|
|
const port = 7001;
|
|
const rateLimit = require("express-rate-limit");
|
|
|
|
app.use(cors());
|
|
app.use(express.urlencoded({ extended: true }));
|
|
app.set("view engine", "ejs");
|
|
app.use(express.json());
|
|
|
|
const limits = {
|
|
time: 1, // = 1 minute
|
|
requests: 10,
|
|
message: "Too many requests from this IP, please try again in 15 minutes",
|
|
};
|
|
|
|
const limiter = rateLimit({
|
|
windowMs: limits.time * 60 * 1000,
|
|
max: limits.requests,
|
|
message: limits.message,
|
|
});
|
|
|
|
app.use(limiter);
|
|
|
|
app.get("/", (req, res) => {
|
|
res.render("index.ejs", { title: port });
|
|
});
|
|
|
|
app.listen(port, () => {
|
|
console.log(`Server is running on port: ${port}`);
|
|
});
|
|
|
|
// error handling code
|
|
app.use((err, req, res, next) => {
|
|
// Log the error stack and send a generic error response
|
|
console.error(err.stack);
|
|
res.status(500).send("Something broke!");
|
|
});
|