-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
29 lines (24 loc) · 988 Bytes
/
index.js
File metadata and controls
29 lines (24 loc) · 988 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const express = require('express');
const MongoClient = require('mongodb').MongoClient;
require('dotenv').config();
const app = express();
const port = process.env.PORT || 3001;
const MDB_USERNAME = process.env.MDB_USERNAME;
const MDB_PASSWORD = process.env.MDB_PASSWORD;
const MDB_URI = `mongodb+srv://${MDB_USERNAME}:${MDB_PASSWORD}@lionshare-7nhlo.mongodb.net/test?retryWrites=true&w=majority`;
const client = new MongoClient(MDB_URI, { useUnifiedTopology: true });
client.connect(function (err, client) {
if (err) throw err;
const collection = client.db('gerry').collection('gerry');
app.get('/state/:state', async function ({ params: { state } }, res, next) {
const results = await collection.find({ state }).limit(1).toArray();
if (results.length === 0) {
next(new Error(`State ${state} not found`));
} else {
res.send(results[0].data);
}
});
});
app.listen(port, () =>
console.log(`Example app listening at http://localhost:${port}`),
);