Hi, I am trying to deploy a react app with lambda functions.
Here’s my repo: GitHub - dyelax/netlify-lambda-test
And deployed url: https://dyelax-lambda-test.netlify.app
For convenience, here is my netlify.toml and the functions in question:
netlify.toml
[build]
functions = "lambda-build"
command = "npm run build"
publish = "build"
[[redirects]]
from = "/e/*"
to = "/.netlify/functions/express"
status = 200
[[redirects]]
from = "/d/*"
to = "/.netlify/functions/default"
status = 200
src/lambda-functions/default.js
import App from '../App.js'
import React from 'react';
import ReactDOMServer from 'react-dom/server';
exports.handler = async (event, context) => {
const app = ReactDOMServer.renderToString(<App path={event.path} />);
return {
statusCode: 200,
body: app,
};
};
src/lambda-functions/express.js
import App from '../App.js'
import React from 'react';
import ReactDOMServer from 'react-dom/server';
import express from 'express'
import serverless from 'serverless-http'
const app = express();
const router = express.Router();
router.get('*', async (req, res) => {
const app = ReactDOMServer.renderToString(<App path={req.path} />);
return res.status(200).send(app);
});
app.use('/.netlify/functions/express', router);
exports.handler = serverless(app);
I have two questions:
- The functions work when I run
netlify dev
locally, but they aren’t working when I deploy.
Locally, the route .netlify/functions/default/123
prints .netlify/functions/default/123
on the screen, as expected. when deployed, the same route says “page not found.” You can test this at https://dyelax-lambda-test.netlify.app/.netlify/functions/default/123.
Is there anything that needs to be set up in particular to deploy lambda functions?
- I want to redirect certain URLs to certain functions. Using netlify dev (where the functions are working), redirects work for the default function syntax but not for Express. I think the issue has to do with the status code not being set correctly, but even if I explicitly set
res.status(200)
in the express function, the redirect doesn’t work. The express function works fine when I navigate directly to.netlify/functions/express
, but usinge/123
, I see “Cannot GET /e/123” and the logs show:
Request from ::1: GET /.netlify/functions/express
Response with status 404 in 38 ms.
Request from ::1: GET /.netlify/functions/express.html
Response with status 404 in 1 ms.
Request from ::1: GET /.netlify/functions/express.htm
Response with status 404 in 0 ms.
Request from ::1: GET /.netlify/functions/express/index.html
Response with status 404 in 32 ms.
Request from ::1: GET /.netlify/functions/express/index.htm
Response with status 404 in 32 ms.