[UPDATE] Can't resolve endpoints and static assets (express and serverless-http)

[Updated 05-08]

Hi there,
It’s my first time setting up an express app with netlify, this is my issue: I was able to set the app properly, however, I’m getting 502 on static files and assets:

https://maidensgaze.netlify.app/api/assets/icons/wiki

# This function has crashed
An unhandled error in the function code triggered the following message:
[TypeError] - Cannot read properties of undefined (reading 'method')

### Stack trace
TypeError: Cannot read properties of undefined (reading 'method')
    at Function.handle (/var/task/functions/server.js:19015:38)
    at router2 (/var/task/functions/server.js:18978:17)
    at Object.<anonymous> (/var/task/functions/server.js:24220:16)
    at Module._compile (node:internal/modules/cjs/loader:1364:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1422:10)
    at Module.load (node:internal/modules/cjs/loader:1203:32)
    at Module._load (node:internal/modules/cjs/loader:1019:12)
    at Module.require (node:internal/modules/cjs/loader:1231:19)
    at require (node:internal/modules/helpers:177:18)
    at Object.<anonymous> (/var/task/server.js:1:18)

This is my site: https://maidensgaze.netlify.app/
This is what I’ve tried so far:

  • changing redirections
  • changing server function several times
  • reinstalling npm
  • updating and downgrading serverless-http
  • adding better webpack config for Prod
  • netlify functions:server (didn’t work either)

This is my setup:
endpoints file:

import express from 'express';
import path from 'path';
import { publicDir } from '../../utils/serverConstants.mjs';

const router = express.Router();

// Serve static files
router.use(express.static(publicDir));
router.use(express.static(models));
router.use(express.static(utils));

router.get('*', (req, res, next) => {
  const filePath = path.resolve(publicDir, 'index.html');
  res.sendFile(filePath);
});

export { router };

server function:

import express from 'express';
import serverless from 'serverless-http';
import {router} from '../src/api/routes/endpoints.mjs';

const app = express();
app.use(express.static('public'));
app.use('/', router);


export const handler = serverless(app);

this is my netlify.toml file:

# example netlify.toml
[build]
  command = "npm run build"
  publish = "public"
  functions = "functions/"

[functions]
  directory = "functions/"
  external_node_modules = ["express"]
  node_bundler = "esbuild"

  ## Uncomment to use this redirect for Single Page Applications like create-react-app.
  ## Not needed for static site generators.
 [[redirects]]
  from = "/api/*"
  to = "/.netlify/functions/server/:splat"
  status = 200
  force = true

[[redirects]]
  from = "/*"
  to = "/index.html"
  status = 200

This is the project structure for these files:

/
-functions
--server.mjs
-public
--index.html
-src
--api
---routes
----endpoints.mjs

I have talked to the AI chatbot, searched through several support docs, checked the documentation for functions and express web app. It’s been a few hours trying to get this to work with no success, I’ve been having different inputs but none of those resolve my static files nor my the endpoint calls.

Let me know how can I further improve my explanation. Any kind of help is highly appreciated since I’m completely blocked.

Thank you!

Hey there,

Thanks for the detailed rundown of what’s going on!

To ensure all necessary files are included in your deployment, you can explicitly specify these in your netlify.toml file using the included_files attribute under the [functions] section. Here’s how you can modify it:

[functions]
  included_files = ["public/assets/**"]

This configuration tells Netlify to include all files within the public/assets directory in the function deployment, ensuring they are available at runtime.

TypeError: Undefined Property Access

The error you’re experiencing (TypeError: Cannot read properties of undefined (reading 'method')) suggests there might be an issue with how the request object is being accessed or manipulated within your Express routes or middleware.

Let us know if you continue to have any issues!