I’m trying to use a Netlify function with Express to setup a few routes that perform different tasks. I have a frontend preact app in the same project.
I could probably accomplish what I need just using separate netlify functions, but I’d much prefer to use Express for this project.
Here’s my function called my-hook.js
import express, { Router } from "express";
import serverless from "serverless-http";
const api = express();
const router = Router();
router.get("/test", (req, res) => res.send(JSON.stringify({ message: "Test finally works!" })));
api.use("/api/", router);
export const handler = serverless(api);
Here’s the toml file
[build]
base = "frontend"
command = "npm run build"
publish = "/build"
[functions]
directory = "functions"
[[redirects]]
from = "/api/*"
to = "/.netlify/functions/:splat"
status = 200
This is a simplified version of the code I used for testing. When I try to reach this endpoint using postman, I get this error:
https://spraywell-suite.netlify.app/api/test
Cannot GET /api/test
I’ve tried many different variations of the URL and routing all to no avail.
However, I have a test route that doesn’t use Express which I can reach no problem.
Any help or insights would be greatly appreciated.