1

// 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.

error screenshot

Mmaxwell
  • 11
  • 2
  • could you please provide your backend and server config code so that we can check what is going on? – rick Aug 04 '23 at 10:54
  • I have added my index.js and app.js above. @rick – Mmaxwell Aug 04 '23 at 11:17
  • the error is pointing to `console.log`. This is more related to the server itself. If you comment out all console works? – rick Aug 04 '23 at 14:25
  • Yes it is related to the server but i am using lambda which is serverless. why does the request fail occasionally, i was thinking DB size could cause it but i increased to rds t2.small (2vCPU, 2Gb ram) still same thing. what else could be causing this – Mmaxwell Aug 05 '23 at 13:29
  • you can start by reading this [Pipe error](https://stackoverflow.com/questions/35641332/node-js-events-js154-throw-err-write-epipe-program-crashing). – rick Aug 07 '23 at 14:03

0 Answers0