-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (50 loc) · 1.57 KB
/
index.js
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const express = require('express');
const mongoose = require('mongoose');
// const sanitize = require('mongo-sanitize');
const app = express();
const mongoURL = 'mongodb://localhost:27017/sample';
const userSchema = new mongoose.Schema({
name: String,
email: String,
admin: Boolean
});
const User = mongoose.model('User', userSchema);
async function connectToDatabase() {
try {
await mongoose.connect(mongoURL, {
useNewUrlParser: true,
useUnifiedTopology: true
});
console.log('Connected to MongoDB');
} catch (err) {
console.error('Failed to connect to MongoDB:', err);
process.exit(1);
}
}
app.get('/', (req, res) => {
res.send('Running MongoDB with Node.js');
});
app.get('/users', async (req, res) => {
await connectToDatabase();
// Sanitize input to avoid NoSQL injection
const email = req.query.email;
try {
// use default query options to avoid NoSQL injection
const query = { $where: `this.email === '${email}'` };
const users = await User.find(query);
if ( users.length === 0 ) {
res.status(404).json({ error: 'User not found' });
return;
}
res.status(200).json(users);
} catch (err) {
console.error('Failed to retrieve users:', err.message);
console.group('Stack trace');
console.error(err);
console.groupEnd();
res.status(500).json({ error: 'Internal server error' });
}
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});