'How do I run an API endpoint deployed to Vercel? (not using Next.js)

I've got a bit of a weird set up here with a codebase that I inherited. It's a CRA app deployed to Vercel but doesn't use Next.js.

Problem: I'm not able to call myapp.com/nonce from Postman or access it in my browser to see the JSON response. There's some configuration stuff that's not quite right, just trying to figure out what that is. Would love any help here!

I have a file structure that looks like this:

Project (create-react-app)
   src
     bunch of React code
   server
     index.js
     package.json
   package.json

(It's not using next.js. If it were, I'd just use the /pages/api/* files)

In my top-level package.json, I have:

"scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build && cd server && npm install && npm run start",
}

and in my /server/package.json I have just a simple start command: node index.js

My server/index.js file:

const path = require('path');
const express = require("express");
const PORT = process.env.PORT || 3001;
const app = express();

app.use(express.static(path.resolve(__dirname, '../MyProject/build')));


app.get('/nonce', async (req, res) => {
  const { address } = req.query;
  const quantity = snapshot[address];

  return res.json({ quantity, address }); // for testing 
})

Edit:

With this as-is, my build phase never actually completes.. It runs a server at 3001 and that's the last log of the Build log

> [email protected] start /vercel/path0/server
> node index.js
Server listening on 3001


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source