-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
59 lines (48 loc) · 1.4 KB
/
app.js
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
58
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const cors = require("cors");
const authRoutes = require("./routes/auth");
const noteRoutes = require("./routes/notes");
const rateLimit = require("express-rate-limit");
const searchRoutes = require("./routes/search");
const throttle = require("express-throttle");
const app = express();
const PORT = process.env.PORT || 3000;
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // limit each IP to 100 requests per windowMs
});
// Apply to all requests
app.use(limiter);
// Middleware
app.use(cors());
app.use(bodyParser.json());
// MongoDB connection
mongoose.connect(
"enter your connection string",
{
useNewUrlParser: true,
useUnifiedTopology: true,
}
);
mongoose.connection.on(
"error",
console.error.bind(console, "MongoDB connection error:")
);
app.get("/", (req, res) => {
res.send("Welcome to the API!");
});
// Routes
app.use("/api/auth", authRoutes);
app.use("/api/notes", throttle({ burst: 10, rate: '5/s' }), noteRoutes);
app.use("/api/search", throttle({ burst: 10, rate: '5/s' }),searchRoutes);
// Error handling middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send("Something went wrong!");
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});