Search code examples
javascriptamazon-web-servicesexpressaws-lambdaserverless

internal server error with when deploying express with serverless


i m facing a problem that i really couldn't resolve for days , I am trying to deploy my express app to AWS using serverless, but when I try to access the endpoint I receive this error

{"message": "Internal server error"}

i really can't figure out the problem also viewed the other similar questions but nothing works

here is my serverless.yml

    service: my-express-application

provider:
  name: aws
  runtime: nodejs14.x
  stage: dev
  region: us-east-1

functions:
  app:
    handler: app.handler
    events:
      - http: ANY /
      - http: 'ANY /{proxy+}'

and this is my app.js

const keys = require("./config/keys");
const activitiesRoutes = require("./routes/activity");
const app = express();
const bodyParser = require("body-parser");
const router = express.Router();
const AWS = require("aws-sdk");
AWS.config.update({
    region: "us-east-1",
});
var docClient = new AWS.DynamoDB.DocumentClient();

// set view engine
app.set("view engine", "ejs");

// set up session cookies
app.use(
    cookieSession({
        maxAge: 24 * 60 * 60 * 1000,
        keys: [keys.session.cookieKey],
    })
);

// initialize passport
 app.use(passport.initialize());
 app.use(passport.session());

// connect to mongodb
mongoose.connect(keys.mongodb.dbURI, () => {
    console.log("connected to mongodb");
});

// set up routes


/


app.listen(3000, () => {
    console.log("app now listening for requests on port 3000");
});
module.exports = serverless(app);

Solution

  • One problem I see it that you are not exporting your handler method correctly. Change this line

    module.exports = serverless(app);
    

    to this:

    module.exports.handler = serverless(app);
    

    (Per this.)