Netlify Express App wth Routes: Function not Found

Hi everybody!

I’m brand new to Netlify and have been struggling with this bug for days now. I’m incorporating Netlify into an existing Express app I have that has routes. But when I try to access anything that isn’t the home page, I keep getting a “Functions not found” error for some reason? Here’s what I have so far…

Project structure:

App.js:


const express = require('express')
const morgan = require('morgan')
const createError = require('http-errors')
require('dotenv').config()
import '../helpers/initMongoDb'
const cors = require('cors')
import ServerlessHttp from 'serverless-http'


import authRoute from "./routes/auth"
import feedRoute from "./routes/feed"
import imageRoute from "./routes/image"
import matchRoute from "./routes/match"
import chatRoute from "./routes/chat"

const app = express()
app.use(morgan('dev'))

const corsOptions = {
    origin: true,
    credentials: true
}
app.options('*', cors(corsOptions))
app.use(express.json({ limit: "50mb", }))
app.use(express.urlencoded({ limit: "50mb", extended: true }))

app.get('/', (req, res) => {
    res.send("hi")
})


app.use('/auth', authRoute)
app.use('/feed', feedRoute)
app.use('/match', matchRoute)
app.use('/chat', chatRoute)
app.use('/images', imageRoute)


app.use(async (req, res, next) => {
    next(createError.NotFound('This route does not exist'))
})

app.use((err, req, res, next) => {
    res.status(err.status || 500)
    res.send({
        error: {
            status: err.status || 500,
            message: err.message
        }
    })
})

const PORT = process.env.PORT || 3007

app.listen(PORT, () => {
    console.log(`server running on port ${PORT}`)
})

const handler = ServerlessHttp(app)
module.exports.handler = async(event, context) => {
    const result = await handler(event, context)
    return result
}

Netlify.toml:

[build]
  functions = "functions"
  node_bundler = "esbuild"

[[redirects]]
  from = "/"
  to = "/.netlify/functions/app"
  status = 200

  [[redirects]]
  from = "/auth/*"
  to = "/.netlify/functions/routes/auth"
  status = 200

 [[redirects]]
  from = "/feed/*"
  to = "/.netlify/functions/routes/feed"
  status = 200

    [[redirects]]
  from = "/matches/*"
  to = "/.netlify/functions/routes/matches"
  status = 200

    [[redirects]]
  from = "/chat/*"
  to = "/.netlify/functions/routes/chat"
  status = 200

    [[redirects]]
  from = "/image/*"
  to = "/.netlify/functions/routes/image"
  status = 200

Any help is greatly appreciated!!

Hey there,

It looks like there may be some confusion here. Netlify functions are designed to handle individual endpoints. However, Express applications typically manage multiple routes within a single server instance. To align with Netlify’s architecture, encapsulate your entire Express app into a single function.

Here’s some helpful links to get you in the right direction: