-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
368 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
const express = require('express'); | ||
const { MongoClient } = require('mongodb'); | ||
const cors = require('cors'); | ||
|
||
const app = express(); | ||
app.use(express.json()); | ||
app.use(cors()); | ||
|
||
const uri = 'mongodb+srv://sparta:[email protected]/?retryWrites=true&w=majority'; | ||
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true }); | ||
|
||
let db, articleCollection, rankCollection, transferCollection; | ||
|
||
async function connectToMongoDB() { | ||
try { | ||
await client.connect(); | ||
console.log('Connected to MongoDB'); | ||
|
||
db = client.db('kickoff'); | ||
articleCollection = db.collection('article'); | ||
rankCollection = db.collection('rank'); | ||
transferCollection = db.collection('transfer'); | ||
} catch (error) { | ||
console.error('Error connecting to MongoDB:', error); | ||
} | ||
} | ||
|
||
connectToMongoDB(); | ||
|
||
const PAGE_SIZE = 3; | ||
|
||
app.get('/article', async (req, res) => { | ||
const pageNumber = req.query.pageNumber ? parseInt(req.query.pageNumber) : 1; | ||
|
||
try { | ||
const articles = await articleCollection | ||
.find({}) | ||
.sort({ _id: -1 }) // _id를 기준으로 역순으로 정렬 | ||
.skip((pageNumber - 1) * PAGE_SIZE) | ||
.limit(PAGE_SIZE) | ||
.toArray(); | ||
|
||
res.json({ result: 'success', article: articles }); | ||
} catch (error) { | ||
console.error('Error fetching articles:', error); | ||
res.status(500).json({ result: 'error', message: 'Internal server error' }); | ||
} | ||
}); | ||
|
||
app.get('/rank', async (req, res) => { | ||
try { | ||
const ranks = await rankCollection.find({}).toArray(); | ||
res.json({ result: 'success', rank: ranks }); | ||
} catch (error) { | ||
console.error('Error fetching ranks:', error); | ||
res.status(500).json({ result: 'error', message: 'Internal server error' }); | ||
} | ||
}); | ||
|
||
app.get('/transfer', async (req, res) => { | ||
try { | ||
const transfers = await transferCollection.find({}).toArray(); | ||
res.json({ result: 'success', transfer: transfers }); | ||
} catch (error) { | ||
console.error('Error fetching transfers:', error); | ||
res.status(500).json({ result: 'error', message: 'Internal server error' }); | ||
} | ||
}); | ||
|
||
const PORT = process.env.PORT || 8000; | ||
app.listen(PORT, () => { | ||
console.log(`Server is running on port ${PORT}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.