// app.js
const app = require("express")();
const http = require("http");
require("dotenv").config();
const bodyParser = require("body-parser");
const userRouter = require("./routes/users/userRoutes");
const amdinRouter = require("./routes/users/adminRoutes");
const cors = require("cors");
const globalErrorHandler = require("./middlewares/globalErrorHandler");
const exportRouter = require("./routes/export/exportRoutes");
const fmmRegsRouter = require("./routes/fmmRegistration/fmmRegsRoutes");
const warehouseRouter = require("./routes/warehouse/warehouseRoutes");
const { API_PREFIX } = process.env;
// Enable KeepAlive to speed up HTTP requests to another microservices
http.globalAgent.keepAlive = true;
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(
cors({
origin: "*",
})
);
//-----------
//Routes
//-----------
//User ruote
app.use(`/${API_PREFIX}/users`, userRouter);
//User ruote
app.use(`/${API_PREFIX}/admin`, amdinRouter);
//Export Documentation route
app.use(`/${API_PREFIX}/export-docs`, exportRouter);
//fmm Registration route
app.use(`/${API_PREFIX}/fmm-registration`, fmmRegsRouter);
//Warehouse route
app.use(`/${API_PREFIX}/warehouse`, warehouseRouter)
app.get(`/${API_PREFIX}/healthcheck`, (req, res) => {
try {
res.send({
uptime: Math.round(process.uptime()),
message: "Working Fine",
timestamp: Date.now(),
});
} catch (e) {
res.status(503).end();
}
});
app.use(globalErrorHandler);
module.exports = app;
// index.js
const app = require('./app')
const { PORT } = process.env
app.listen(PORT, () => console.log(`Server up and running on ${PORT}`));
module.exports = app;
Sometimes when I hit my endpoint, it gives 502 response. I even configured a cron event that wakes my lambda function every 4 mins. Even with that cron, some requests still give the 502 occasionally . this is the error i see on cloudwatch:
2023-08-04T09:17:20.041Z 2357fed9-0aec-463d-b696-f7c88ba975db INFO ERROR: @vendia/serverless-express connection error 2023-08-04T09:17:20.041Z 2357fed9-0aec-463d-b696-f7c88ba975db ERROR Error: write EPIPE at afterWriteDispatched (node:internal/stream_base_commons:160:15) at writevGeneric (node:internal/stream_base_commons:143:3) at Socket._writeGeneric (node:net:903:11) at Socket._writev (node:net:912:8) at doWrite (node:internal/streams/writable:408:12) at clearBuffer (node:internal/streams/writable:563:5) at Socket.Writable.uncork (node:internal/streams/writable:350:7) at ClientRequest._flushOutput (node:_http_outgoing:1094:10) at ClientRequest._flush (node:_http_outgoing:1063:22) at onSocketNT (node:_http_client:894:9) { errno: -32, code: 'EPIPE', syscall: 'write' } END RequestId: 2357fed9-0aec-463d-b696-f7c88ba975db
I have searched everywhere for solution but couldn’t find.
I expect to see no errors anytime i hit the api because of the cron already in place to wake my lambda function.