Not found 404 when deploy nodejs vercel

I has my index.js file , I write with nodejs.

const express = require("express");
const cors = require("cors");
const app = express();
const port = 5000;
const bodyParser = require("body-parser");
const zego = require("./offline_modules/zegoServerAssistant");
const fs = require("fs");

app.use(cors());
app.use(bodyParser.json());
require("dotenv").config();

app.get("/token/:userId", async (req, res, next) => {
  const appId = process.env.appId;
  const userId = req.params.userId;
  const secret = process.env.secret;

  const effectiveTimeInSeconds = 2 * 60;

  const payloadObject = {
    room_id: userId,

    privilege: {
      1: 1, // loginRoom
      2: 0, // publishStream
    },
    stream_id_list: null,
  };
  try {
    const token = zego.generateToken04(
      appId,
      userId,
      secret,
      effectiveTimeInSeconds,
      JSON.stringify(payloadObject)
    );
    res.json({ token });
  } catch (e) {
    console.log({ e });
    res.json({ e: e?.errorMessage });
  }
});

app.listen(port, () => {
  console.log(`Server running in : ${port}`);
});

This is my package.json, I have fix with the guide from google that put the

"engines": { "node": "14.x" },

{
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "engines": {
    "node": "14.x"
  },
  "scripts": {
    "start": "nodemon index.js"
  },
  
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.20.2",
    "cors": "^2.8.5",
    "dotenv": "^16.0.3",
    "express": "^4.18.2"
  },
  "devDependencies": {
    "nodemon": "^2.0.21"
  }
}

And this is my vercel.json

{
    "version": 2,
    "builds": [
      {
        "src": "./index.js",
        "use": "@vercel/node"
      }
    ],
    "routes": [
      
      {
        "src": "/(.*)",
        "dest": "index.js"
      }
    ]
  }

And this is my file structure enter image description here

When I deploy on vercel, the 404 page not found occurs. I been searching for a while but no working, so please help me with this. Thanks you everyone.

Upvotes: 1

Views: 205

Answers (0)

Related Questions