Touble getting local development to match deploy using Netlify functions

I’m unable to access custom functions in local development yet it seems to work once deployed on netlify.

I started by cloning the git repo for Express Functions: Netlify Functions Express.

I then deployed this to Netlify.
Netlify site: https://quizzical-mccarthy-88f3b3.netlify.app/

For local development, I used netlify link to link my site. I ran npm install to install dependencies and then ntl dev to start it up locally. It seems to work the same as the live site (although the AWS Serverless example crashes the development server, but I’m not interested in that).

I then wrote a new function called api.js intended to replicate the serverless-http function’s /hello/ route.

/* Express App */
import express from "express";
import cors from "cors";
import morgan from "morgan";
import bodyParser from "body-parser";
import compression from "compression";

import serverless from "serverless-http";

/* My express App */

const app = express();
const router = express.Router();

// gzip responses
router.use(compression());

router.get("/hello/", function (req, res) {
  res.send("hello world");
});

// Attach logger
app.use(morgan("dev"));

// Setup routes
app.use("/.netlify/functions/api/", router);

// Apply express middlewares
router.use(cors());
router.use(bodyParser.json());
router.use(bodyParser.urlencoded({ extended: true }));

// Export lambda handler
exports.handler = serverless(app);

Now when I start up a local development server using ntl dev the /.netlify/functions/api/hello does not return the response.

However, the response from the existing functions still works.

Once I deploy the site to netlify, my custom function works as expected. Hello route on the web

What am I missing here? It doesn’t help that the repo for Express Functions does not seem to offer guidance for local development.

Update with resolution: for local development, the route should be simply the function name (e.g. /api/) rather than the /.netlify/functions/api path. This is accounted for with the routerBasePath variable.

const routerBasePath =
process.env.NODE_ENV === "dev" ? `/api` : `/.netlify/functions/api/`;

And then setting that as the path for the function. app.use(routerBasePath, router);

thanks for posting your solution, @JonoMacC!