Home > Software design >  serverless route works twice and then crashes - address already in use :::3000
serverless route works twice and then crashes - address already in use :::3000

Time:12-31

I can't figure out why a serverless route works twice then I get
address already in use :::3000 and it crashes. It returns the response body back into postman the 2nd time and then crashes.

let me know if any other info is needed. I appreciate any help!

Error

Error: listen EADDRINUSE: address already in use :::3000
    at Server.setupListenHandle [as _listen2] (node:net:1330:16)
    at listenInCluster (node:net:1378:12)
    at Server.listen (node:net:1465:7)
    at Function.listen (/Users/phil/Documents/code/Phil App/app/backend/node_modules/express/lib/application.js:618:24)
    at Object../src/app.ts (/Users/phil/Documents/code/Phil App/app/backend/dist/service/src/handler.js:116:5)
    at __webpack_require__ (/Users/phil/Documents/code/Phil App/app/backend/dist/service/src/handler.js:21:30)
    at Object../src/handler.ts (/Users/phil/Documents/code/Phil App/app/backend/dist/service/src/handler.js:137:31)
    at __webpack_require__ (/Users/phil/Documents/code/Phil App/app/backend/dist/service/src/handler.js:21:30)
    at /Users/phil/Documents/code/Phil App/app/backend/dist/service/src/handler.js:85:18
    at Object.<anonymous> (/Users/phil/Documents/code/Phil App/app/backend/dist/service/src/handler.js:88:10)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at /Users/phil/Documents/code/Phil App/app/backend/node_modules/serverless-offline/dist/lambda/handler-runner/in-process-runner/InProcessRunner.js:157:133
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async InProcessRunner.run (/Users/phil/Documents/code/Phil App/app/backend/node_modules/serverless-offline/dist/lambda/handler-runner/in-process-runner/InProcessRunner.js:157:9)

Very basic route

import bodyParser from 'body-parser';

const router = express.Router({ mergeParams: true });

router.use(bodyParser.json({ strict: false }));

router.post('/test', async (req, res: Response) => {
    console.log('request', req.body);
    return res.status(200).json({ test: 'success' });
});

export default router;

app.ts

import cors from 'cors';
import express from 'express';
import routers from './routes';
const app = express();

app.use(cors({ maxAge: 43200 }));
app.use(
    express.json({
        verify: (req: any, res: express.Response, buf: Buffer) => {
            req.rawBody = buf;
        }
    })
);

app.use('/testRoute', routers.testRouter);

const PORT = 3000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));

export default app;

handler

import { APIGatewayProxyEvent, Context } from 'aws-lambda';
import awsServerlessExpress from 'aws-serverless-express';
import app from './app';

const logger = require('./utils/logger');
const log = logger.getLogger();
log.setName('handler');

const server = awsServerlessExpress.createServer(app);

export function service(event: APIGatewayProxyEvent, context: Context) {
    return awsServerlessExpress.proxy(server, event, context);
}

CodePudding user response:

Problem

awsServerlessExpress.proxy can create multiple instances of the server

You've used listen method on the server

Two instances try to setup at the same port which causes the error

Solution

Remove listen method, it's unnecessary, aws handles listening for you

  • Related