Add /api/posts/ route
This commit is contained in:
@@ -2,6 +2,10 @@ const express = require('express')
|
|||||||
|
|
||||||
const app = express()
|
const app = express()
|
||||||
|
|
||||||
|
const posts = require('./routes/api/posts')
|
||||||
|
|
||||||
|
app.use('/api/posts', posts)
|
||||||
|
|
||||||
const port = process.env.PORT || 5000
|
const port = process.env.PORT || 5000
|
||||||
|
|
||||||
app.listen(port, () => console.log(`Server started on port ${port}`))
|
app.listen(port, () => console.log(`Server started on port ${port}`))
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
const express = require('express')
|
||||||
|
const mongodb = require('mongodb')
|
||||||
|
|
||||||
|
const router = express.Router()
|
||||||
|
|
||||||
|
const DB_NAME = process.env.DB_NAME || 'test'
|
||||||
|
const URI = process.env.URI || 'mongodb://localhost:27017'
|
||||||
|
|
||||||
|
async function getPostCollection() {
|
||||||
|
const client = await mongodb.MongoClient.connect(URI, {
|
||||||
|
useNewUrlParser: true,
|
||||||
|
useUnifiedTopology: true,
|
||||||
|
})
|
||||||
|
return client.db(DB_NAME).collection('posts')
|
||||||
|
}
|
||||||
|
|
||||||
|
router.get('/', async (req, res) => {
|
||||||
|
const posts = await getPostCollection()
|
||||||
|
res.send(await posts.find({}).toArray())
|
||||||
|
})
|
||||||
|
|
||||||
|
router.get('/:id', async (req, res) => {
|
||||||
|
const re = /[0-9A-Fa-f]{24}/g
|
||||||
|
|
||||||
|
if (!re.test(req.params.id)) {
|
||||||
|
res.status(404).send('Sorry, can not find that')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const posts = await getPostCollection()
|
||||||
|
res.send(await posts.find({ _id: mongodb.ObjectID(req.params.id) }).toArray())
|
||||||
|
})
|
||||||
|
|
||||||
|
module.exports = router
|
||||||
Reference in New Issue
Block a user