First time - node server not loading file

I have a react front end - which seems to be working and a express backend. The backend project is deploying but crashes. It seems to me that it is not loading a file in the root directory. I am attaching the code for loading the file and the code I added to the vercel.json file.

Is there something I need to change to get it to load? Is that the problem I am having?

Thank you.


    const map = await new Promise((resolve, reject)=>{
        tmx.parseFile('snowmap1.tmx', (err, loadedMap) => {
            if(err) return reject(err)
            resolve(loadedMap)
        })
    })
{
    "version": 2,
    "builds": [{ "src": "./index.js", "use": "@vercel/node"}],
    "routes": [{ "src": "/(.*)", "dest": "/"}]
}

If you’re having trouble deploying an Express app, this guide can help.

You can also ask v0 for suggestions tailored to your own project setup.

Hi, @hedkace! Welcome to the Vercel Community :waving_hand:

Nothing immediately jumped out to me as an issue, could you share a minimal reproducible example with us?

I’ll also share this thread in case it’s helpful!

1 Like

“could you share a minimal reproducible example with us?”

Yes. But I’m not sure what I should share. Do you want to see the source code or something?

Thank you.

Apologises! This should help → How do I create a minimal reproducible example for Vercel Support?

1 Like

Okay. I tried to do that.
Here is the project: https://mre1.vercel.app/
The deployment: https://vercel.com/hedkaces-projects/mre1/2oi5w7scC1HfHDEdZnoY9CLe8ZHp
The github repo is here: GitHub - hedkace/mre1: Example

Any help would be appreciated. Thank you again for looking at this!

for some reason, require was not working, so it said to use import instead. So I changed it to that for this example.

Hi @hedkace, I was able to get your app working (try here) with the following changes:

  • move the index.js in api/index.js file
  • remove legacy options from the vercel.json
  • update index.js to correctly handle file paths
import express from "express";
import { parseFile } from "tmx-parser";
import { join } from "path";

// Create Express app
const app = express();
app.use(express.json());

// Helper function to parse TMX file using promises
async function parseTmxFile(filePath) {
  return new Promise((resolve, reject) => {
    parseFile(filePath, (err, loadedMap) => {
      if (err) return reject(err);
      resolve(loadedMap);
    });
  });
}

async function loadMap() {
  try {
    // Use a path relative to the function's directory
    const filePath = join(__dirname, "..", "snowmap1.tmx");
    mapCache = await parseTmxFile(filePath);
    return mapCache;
  } catch (error) {
    console.error("Error loading map:", error);
    throw error;
  }
}

// Define routes
app.get("/test", (req, res) => {
  res.json({ message: "test ok" });
});

app.get("/", async (req, res) => {
  try {
    const map = await loadMap();
    console.log({ map });
    res.json({
      data: { version: map.version, width: map.width, height: map.height },
    });
  } catch (error) {
    res.status(500).json({ success: false, error: error.message });
  }
});

app.listen(3000, () => console.log("Server ready on port 3000."));

module.exports = app;
{
  "rewrites": [{ "source": "/(.*)", "destination": "/api/index" }]
}
3 Likes

Thank you very much! The server is not crashing anymore. I am still having some trouble though that did help me make some progress.

My new issue is this: i seem to not be able to access mongodb or use socketio. No idea what’s going on. I don’t think it’s a ip whitelist issue but I could be wrong.

Any ideas?

1 Like

Hi @hedkace, I’m glad you got it working.

About the Atlas issue: it is what the error says, your Atlas instance doesn’t know about Vercel. See this documentation to resolve it.

1 Like

Thank you! I couldn’t look at it for a few days there, but I just followed that link and it is working for logging in.

I have one more issue. I am trying to use socket.io and that seems to be the only thing left to fix.

Does vercel let you use web sockets?

Yes! We have a list of providers who can help you integrate realtime communication in your functions

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.