Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript Example #28

Open
kimfucious opened this issue Aug 10, 2022 · 1 comment
Open

TypeScript Example #28

kimfucious opened this issue Aug 10, 2022 · 1 comment

Comments

@kimfucious
Copy link

Hi there,

I'm trying to use your fine docs to run Express as a serverless function on Netlify, but my function file is written in TypeScript, which is necessary for my use case.

I've tried:

const handler = serverless(app);
export { handler };

Unfortunately, this results in a 404 when I try to do a get on a route when the function is being served by netlify functions:serve, using the Netlify CLI.

Am I missing a step here? Did I do the export wrong? A TypeScript example would be very useful here.

Thanks!

@kimfucious
Copy link
Author

Okay, it took me a while, but it appears I was missing an important thing, and the export wasn't the issue.

const app = express();
const router = express.Router();
const functionName = "api";
const routerBasePath =
    process.env.NODE_ENV === "development"
        ? `/${functionName}`
        : `/.netlify/functions/${functionName}/`;
app.use(routerBasePath, router);
router.use(cors());

If I don't have the routerBasePath setup with the function name, things don't work.

Frankly, I have no idea why that's needed, so any explanation around that would be enlightening.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant