Deploying express serverless functions along with react app

Hello everyone,
I am trying to deploy the express serverless function along with the react app but it isn’t happening.
This is how I’ve initialized the serverless APIs. I’ve created a separate folder to install packages related to the server only.

Code of api.ts

import express, { Router } from 'express';
import serverless from 'serverless-http';

const api = express();
console.log('api', api);
const router = Router();
router.use('/', (req, res, next) => {
	console.log('i am inside server');
router.get('/hello', (req, res) => res.send('Hello World!'));

api.use('/api/', router);

export const handler = serverless(api);

These are all the folders I have.

Last is the netlify.toml file

  command = "npm run build"
  functions = "netlify/functions/"
  publish = "build"

  external_node_modules = ["express"]
  node_bundler = "esbuild"
  from = "/api/*"
  to = "/.netlify/functions/:splat"
  status = 200
  force = true

After all this, I am still not able to access the API. For ref : GitHub - ardourApeX/netlify-serverless-function at feat/express

what site are you trying to configure this for?

My whole approach is to use react to develop the frontend and to use the express app as a serverless function where I will specify other routes. The issue I am facing is I am not able to access the api using the command netlify dev.
Here is the repo link : GitHub - ardourApeX/netlify-serverless-function at feat/test-express

Hmm, it’s odd. I’m having the same issue when I clone your repo. I even tried simplifying it, and it’s still not working, but when I changed the api.js to the following, it works:

exports.handler = async function(event, context) {
  return {
    statusCode: 200,
    body: JSON.stringify({ message: "Hello World from serverless function!" })

So while it may not resolve the issue it does show the Netlify.toml, and the folder paths are configured correctly at least. it seems to be an issue with express and how it’s configured, maybe?

@joygupta, I tedted your repo and the only required change was to change the redirect to: /.netlify/functions/hello/:splat. Everything then works as intended.

1 Like