-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js.txt
55 lines (41 loc) · 1.19 KB
/
app.js.txt
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
// app.js
const express = require('express');
const app = express();
const port = 3000;
// 미들웨어 설정 (CORS 처리)
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// 라우팅
app.get('/', (req, res) => {
res.send('Hello, World!');
});
// 서버 실행
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
// app.js
const mongoose = require('mongoose');
// ...
// MongoDB 연결
mongoose.connect('mongodb://localhost:27017/restaurant-info', {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log('Connected to MongoDB'))
.catch(err => console.error('Error connecting to MongoDB:', err));
// ...
// app.js
const Restaurant = require('./models/restaurant');
// ...
// 레스토랑 정보 검색 엔드포인트
app.get('/restaurants', async (req, res) => {
try {
const restaurantName = req.query.name;
const restaurants = await Restaurant.find({ name: { $regex: restaurantName, $options: 'i' } });
res.json(restaurants);
} catch (err) {
console.error('Error while searching restaurants:', err);
res.status(500).json({ error: 'Error while searching restaurants' });
}
});
// ...