Out of boredom, I decided to create a tap game like NotCoin.
Everything works fine on local sevrera, but when I deployed it on Vercel, the API gateways stopped working.
[error 1]
https://tapgame-tg.vercel.app/api/update
The page could not be found
NOT_FOUND
[error 2]
https://tapgame-tg.vercel.app/api/user/1473999790
The page could not be found
NOT_FOUND
How I can fix this?
index.js
const express = require('express');
const mongoose = require('mongoose');
const dotenv = require('dotenv');
const path = require('path');
const cors = require('cors');
dotenv.config();
const app = express();
app.use(express.static('public'));
app.use(express.json());
mongoose.connect(process.env.MONGODB_URI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log('MongoDB connected'))
.catch(err => {
console.error('MongoDB connection error:', err);
process.exit(1);
});
mongoose.connection.on('error', err => {
console.error(`Mongoose connection error: ${err}`);
});
const User = require('./models/User');
app.use('/api/user', require('./api/routes/user'));
app.use('/api/update', require('./api/routes/update'));
app.use(express.static(path.join(__dirname, 'public')));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send('Something broke!');
});
app.use((req, res, next) => {
console.log(`Request URL: ${req.url}`);
next();
});
app.use(cors());
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
file where error 1 awake
api/routes/user.js
const express = require('express');
const User = require('../../models/User');
const router = express.Router();
router.get('/:telegramId', async (req, res, next) => {
const telegramId = req.params.telegramId;
console.log(`Fetching data for user with telegramId: ${telegramId}`);
try {
const user = await User.findOne({ telegramId });
if (!user) {
console.log(`User with telegramId: ${telegramId} not found. Creating new user.`);
user = new User({ telegramId });
await user.save();
}
res.json(user);
} catch (error) {
console.error('Error fetching user data:', error);
next(error);
}
});
module.exports = router;
file where error 2 awake
api/routes/update.js
const express = require('express');
const router = express.Router();
const User = require('../../models/User');
router.post('/', async (req, res) => {
const { telegramId, coins, upgradeLevel } = req.body;
try {
const user = await User.findOneAndUpdate(
{ telegramId },
{ coins, upgradeLevel },
{ new: true }
);
if (!user) {
return res.status(404).json({ message: 'User not found' });
}
res.json(user);
} catch (error) {
console.error('Error updating user data:', error);
res.status(500).json({ message: 'Internal server error' });
}
});
module.exports = router;
Tried to change some routes to the API, but it’s not working.