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.
Change your vercel.json file:
{ "builds": [ { "src": "index.js", "use": "@vercel/node" } ], "routes": [ { "src": "/.*", "dest": "index.js" } ]}
Change target from "/" to "index.js"