-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (48 loc) · 1.58 KB
/
index.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
import dotenv from "dotenv";
dotenv.config();
import express from "express";
import serverless from "serverless-http";
import cors from "cors";
import mongoose from "mongoose";
import compression from "compression";
import songsRoute from "./routes/songsRoute.js";
import authRoute from "./routes/authRoute.js";
import reportsRoute from "./routes/reportsRoute.js";
const app = express();
// https://awstip.com/deploying-an-express-js-mongodb-application-on-aws-lambda-with-serverless-framework-289c39891a3f
// forever thankfull mr Afeef Razick
app.use(async (req, res, next) => {
//const client
if (mongoose.connection.readyState === 0) {
await mongoose
.connect(process.env.MONGO_URI)
.then(() => {
console.log("Connected to MongoDB server successfully");
})
.catch((err) => {
console.log(err);
});
} else {
console.log(
"Connection already established, reusing the existing connection",
);
}
return next();
});
app.use(express.json());
// catch bad json syntax
app.use((err, req, res, next) => {
if (err instanceof SyntaxError && err.status === 400 && "body" in err)
return res.status(400).json({ status: 400, message: err.message });
next();
});
app.use(cors());
app.use(compression()); // doesnt work
app.use("/songs", songsRoute);
app.use("/login", authRoute);
app.use("/reports", reportsRoute);
const _handler = serverless(app);
export const handler = async (event, context) => {
context.callbackWaitsForEmptyEventLoop = false; // caches mongoose connections inbetween requests
return await _handler(event, context);
};