diff --git a/scripts/setup.js b/scripts/setup.js index f614e4d..dff3ff1 100644 --- a/scripts/setup.js +++ b/scripts/setup.js @@ -8,7 +8,7 @@ console.log('🚀 Setting up StackIt Q&A Platform...\n') // Check if Node.js version is compatible const nodeVersion = process.version -const majorVersion = parseInt(nodeVersion.slice(1).split('.')[0]) +const majorVersion = parseInt(nodeVersion.slice(1, 10).split('.')[0]) if (majorVersion < 16) { console.error('❌ Node.js version 16 or higher is required') console.error(`Current version: ${nodeVersion}`) diff --git a/server/index.js b/server/index.js index 71006bd..f289ce5 100644 --- a/server/index.js +++ b/server/index.js @@ -65,7 +65,7 @@ const shouldEnableRateLimit = () => { const getRateLimitMax = () => { // Environment variable override takes precedence if (process.env.RATE_LIMIT_MAX) { - return parseInt(process.env.RATE_LIMIT_MAX); + return parseInt(process.env.RATE_LIMIT_MAX, 10); } // Production: 100 requests per 15 minutes diff --git a/server/routes/notifications.js b/server/routes/notifications.js index 4f6b81a..9670483 100644 --- a/server/routes/notifications.js +++ b/server/routes/notifications.js @@ -6,8 +6,8 @@ const Notification = require('../models/Notification') // Get user's notifications router.get('/', authenticateToken, async (req, res) => { try { - const page = parseInt(req.query.page) || 1 - const limit = parseInt(req.query.limit) || 20 + const page = parseInt(req.query.page, 10) || 1 + const limit = parseInt(req.query.limit, 10) || 20 const skip = (page - 1) * limit const notifications = await Notification.find({ recipient: req.user._id }) diff --git a/server/routes/questions.js b/server/routes/questions.js index 9d64260..f126d54 100644 --- a/server/routes/questions.js +++ b/server/routes/questions.js @@ -108,14 +108,14 @@ router.get('/', async (req, res) => { .populate('author', 'username reputation avatar') .sort(sortOption) .skip(skip) - .limit(parseInt(limit)) + .limit(parseInt(limit, 10)) .lean() const total = await Question.countDocuments(query) const totalPages = Math.ceil(total / limit) // Add virtual fields - const questionsWithVirtuals = questions.map(q => ({ + const questionsWithVirtuals = (questions ?? []).map(q => ({ ...q, voteCount: q.votes.upvotes.length - q.votes.downvotes.length, totalVotes: q.votes.upvotes.length + q.votes.downvotes.length, @@ -126,7 +126,7 @@ router.get('/', async (req, res) => { res.json({ questions: questionsWithVirtuals, pagination: { - currentPage: parseInt(page), + currentPage: parseInt(page, 10), totalPages, total, hasNext: page < totalPages, diff --git a/server/routes/tags.js b/server/routes/tags.js index 17e5140..b7a19c0 100644 --- a/server/routes/tags.js +++ b/server/routes/tags.js @@ -21,7 +21,7 @@ router.get('/', async (req, res) => { const tags = await Tag.find(query) .sort({ questionCount: -1, name: 1 }) - .limit(parseInt(limit)) + .limit(parseInt(limit, 10)) res.json(tags) } catch (error) { @@ -34,7 +34,7 @@ router.get('/', async (req, res) => { router.get('/popular', async (req, res) => { try { const { limit = 10 } = req.query - const tags = await Tag.getPopularTags(parseInt(limit)) + const tags = await Tag.getPopularTags(parseInt(limit, 10)) res.json(tags) } catch (error) { console.error('Error fetching popular tags:', error) @@ -46,7 +46,7 @@ router.get('/popular', async (req, res) => { router.get('/trending', async (req, res) => { try { const { limit = 10 } = req.query - const tags = await Tag.getTrendingTags(parseInt(limit)) + const tags = await Tag.getTrendingTags(parseInt(limit, 10)) res.json(tags) } catch (error) { console.error('Error fetching trending tags:', error) @@ -63,7 +63,7 @@ router.get('/search', async (req, res) => { return res.status(400).json({ message: 'Search query is required' }) } - const tags = await Tag.searchTags(q, parseInt(limit)) + const tags = await Tag.searchTags(q, parseInt(limit, 10)) res.json(tags) } catch (error) { console.error('Error searching tags:', error) @@ -127,7 +127,7 @@ router.get('/:name/questions', async (req, res) => { .populate('author', 'username') .sort(sortOption) .skip(skip) - .limit(parseInt(limit)) + .limit(parseInt(limit, 10)) const total = await Question.countDocuments({ tags: tag.name }) @@ -135,8 +135,8 @@ router.get('/:name/questions', async (req, res) => { tag, questions, pagination: { - page: parseInt(page), - limit: parseInt(limit), + page: parseInt(page, 10), + limit: parseInt(limit, 10), total, pages: Math.ceil(total / limit) }