Fix homepage API 500 by using axios baseURL and stable paginate.
deploy to danak / build_and_deploy (push) Has been cancelled

Replace self-proxy with direct SSR baseURL, rewrite blog post pagination with async/await, pin mongoose-paginate-v2, and add API error logging.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-07-03 16:05:16 +03:30
parent 3c062cefb6
commit 6bb4719e99
6 changed files with 54 additions and 45 deletions
+5 -6
View File
@@ -72,11 +72,10 @@ export default {
}, },
axios: { axios: {
proxy: true // SSR calls the integrated Express API directly; browser uses relative /api paths.
}, proxy: false,
proxy: { baseURL: `http://127.0.0.1:${nuxtPort}`,
// SSR axios requests must hit the same Nuxt process that hosts serverMiddleware (/api). browserBaseURL: '/'
'/api': { target: `http://127.0.0.1:${nuxtPort}` }
}, },
router: { router: {
linkActiveClass: 'active-link', linkActiveClass: 'active-link',
@@ -85,7 +84,7 @@ export default {
serverMiddleware: ['~/server/index.js'], serverMiddleware: ['~/server/index.js'],
server: { server: {
host: '0.0.0.0', host: '0.0.0.0',
port: 3000 port: nuxtPort
}, },
auth: { auth: {
+3 -21
View File
@@ -12522,21 +12522,6 @@
"url": "https://opencollective.com/mongoose" "url": "https://opencollective.com/mongoose"
} }
}, },
"node_modules/mongoose-lean-virtuals": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/mongoose-lean-virtuals/-/mongoose-lean-virtuals-1.1.1.tgz",
"integrity": "sha512-8chOqpVE3bcoWT2pIgcJeIZlXaOfQCavZgQZF4qytUtjRBqsNMyzUoR16qdw9XL2kC478N8iA8z0AA+NSS0d1A==",
"license": "Apache 2.0",
"dependencies": {
"mpath": "^0.8.4"
},
"engines": {
"node": ">=16.20.1"
},
"peerDependencies": {
"mongoose": ">=5.11.10"
}
},
"node_modules/mongoose-legacy-pluralize": { "node_modules/mongoose-legacy-pluralize": {
"version": "1.0.2", "version": "1.0.2",
"resolved": "https://registry.npmjs.org/mongoose-legacy-pluralize/-/mongoose-legacy-pluralize-1.0.2.tgz", "resolved": "https://registry.npmjs.org/mongoose-legacy-pluralize/-/mongoose-legacy-pluralize-1.0.2.tgz",
@@ -12547,13 +12532,10 @@
} }
}, },
"node_modules/mongoose-paginate-v2": { "node_modules/mongoose-paginate-v2": {
"version": "1.9.5", "version": "1.3.18",
"resolved": "https://registry.npmjs.org/mongoose-paginate-v2/-/mongoose-paginate-v2-1.9.5.tgz", "resolved": "https://registry.npmjs.org/mongoose-paginate-v2/-/mongoose-paginate-v2-1.3.18.tgz",
"integrity": "sha512-sgPbV0G7SfdW1XdEuNMokzJOT85GNx786itp6UiNmDvKGyxMSHbeyn4tMZBseOmGu2uOo5ByVgfjhaxkOBY71A==", "integrity": "sha512-MTEyXvQmUNlXyPGophxILHTYQQ80r3uMtgdnKVr+4qgWLM6JxHbWsFpCmaJJ7ofuV7bhkfBTGx3EDoJo+bIKFw==",
"license": "MIT", "license": "MIT",
"dependencies": {
"mongoose-lean-virtuals": "^1.1.0"
},
"engines": { "engines": {
"node": ">=4.0.0" "node": ">=4.0.0"
} }
+6 -1
View File
@@ -1030,7 +1030,12 @@ export default {
blogPosts: blogPosts.data blogPosts: blogPosts.data
} }
} catch (e) { } catch (e) {
console.error('Homepage asyncData failed:', e.message || e) console.error('Homepage asyncData failed:', {
url: e.config?.url,
status: e.response?.status,
data: e.response?.data,
message: e.message
})
error({ statusCode: 500, message: 'there is a problem here' }) error({ statusCode: 500, message: 'there is a problem here' })
} }
} }
+30 -16
View File
@@ -112,25 +112,39 @@ module.exports.getAll = [
// '/api/public/blogPosts?limit=4' --> gets only 4 published post of all categories // '/api/public/blogPosts?limit=4' --> gets only 4 published post of all categories
// '/api/public/blogPosts?category=${encodeURIComponent(categoryTitle)}&page=${pageNumber}' --> gets posts depend on the category title and in chunked data // '/api/public/blogPosts?category=${encodeURIComponent(categoryTitle)}&page=${pageNumber}' --> gets posts depend on the category title and in chunked data
if (req.query.category) { try {
try { if (req.query.category) {
const categoryID = await BlogCategory.findOne({$or: [{'locale.fa.title': req.query.category}, {'locale.en.title': req.query.category}]}) const filter = {published: true}
const filter = req.query.category === 'all' ? {published: true} : {published: true, category: categoryID._id} if (req.query.category !== 'all') {
BlogPost.paginate(filter, {page: req.query.page, limit: blogPaginateLimit, populate: 'category', sort: {_id: -1}}, (err, posts) => { const category = await BlogCategory.findOne({
if (err) return res500(res, {message: _sr['fa'].not_found.item_id}) $or: [
return res.json(posts) {'locale.fa.title': req.query.category},
{'locale.en.title': req.query.category}
]
})
if (!category) return res404(res, _sr['fa'].not_found.item_id)
filter.category = category._id
}
const posts = await BlogPost.paginate(filter, {
page: Number(req.query.page) || 1,
limit: blogPaginateLimit,
populate: 'category',
sort: {_id: -1}
}) })
} catch (e) { return res.json(posts)
return res500(res, e)
} }
} else {
let query = {published: true} const query = {published: true}
if (req.query.getAll) delete query.published if (req.query.getAll) delete query.published
const limit = Number(req.query.limit) || false const limit = Number(req.query.limit) || 0
BlogPost.find(query).sort({_id: -1}).limit(limit).populate('category').exec((err, posts) => { const posts = await BlogPost.find(query)
if (err) return res500(res, err) .sort({_id: -1})
else return res.json(posts) .limit(limit)
}) .populate('category')
return res.json(posts)
} catch (e) {
console.error('blogPost getAll error:', e)
return res500(res, e)
} }
} }
] ]
+8
View File
@@ -25,6 +25,14 @@ app.use('/public', Public)
app.use('/auth', auth) app.use('/auth', auth)
app.use('/admin', isAdmin, admin) app.use('/admin', isAdmin, admin)
app.get('/health', (req, res) => {
res.json({ ok: true })
})
app.use((err, req, res, next) => {
console.error('Express error:', req.method, req.originalUrl, err?.message || err)
res.status(500).json({ message: err?.message || 'Internal Server Error' })
})
// Export the server middleware // Export the server middleware
module.exports = { module.exports = {
+2 -1
View File
@@ -3,7 +3,8 @@ module.exports.res404 = (res, err) => {
} }
module.exports.res500 = (res, err) => { module.exports.res500 = (res, err) => {
return res.status(500).json({message: err}) console.error('API 500:', err?.message || err)
return res.status(500).json({message: err?.message || err})
} }
module.exports.checkValidations = validationResult => { module.exports.checkValidations = validationResult => {