When I try to refresh react app page for "/login" it shows error Unable to get /login
P粉819533564
P粉819533564 2024-04-02 15:17:42
0
1
442

When I refresh the page linked to /login, I get the error "Unable to get /login". I have a React application hosted on Vercel. Below is the attached screenshot. Error page

My index.js file code is:

const connectToMongo = require('./db');
const express = require('express')
var cors = require('cors')

connectToMongo();

const app = express()
const port = process.env.PORT || 5000

app.use(express.json()) // this middleware is used if you want to send request through body req.body
app.use(cors());

// available routes
app.use('/api/auth', require('./routes/auth'));
app.use('/api/notes', require('./routes/notes'));


if (process.env.NODE_ENV === 'production') {
  
  const path = require('path')

  app.get('/', (req, res) => {
    app.use(express.static(path.join(__dirname, 'client', 'build')))
    res.sendFile(path.join(__dirname, 'client', 'build', 'index.html'))
  });
}

app.listen(port, () => {
  console.log(`iNotebook backend app listening on port ${port}`)
})

My vercel.json file code is:

{
    "builds": [
        {
            "src": "./index.js",
            "use": "@vercel/node"

        }
    ],
    "routes": [
        {
            "src": "/.*",
            "dest": "/"
        }
    ]
}

I'm trying to refresh the page but it's showing an error. However, it should display the login page.

P粉819533564
P粉819533564

reply all(1)
P粉154228483

Change your vercel.json file:

{
"builds": [
    {
        "src": "index.js",
        "use": "@vercel/node"

    }
],
"routes": [
    {
        "src": "/.*",
        "dest": "index.js"
    }
]

}

Change target from "/" to "index.js"

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template