-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
105 lines (84 loc) · 2.34 KB
/
server.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import process from 'node:process';
import dotenv from 'dotenv-defaults';
import express from 'express';
import cors from 'cors';
import path from 'path';
import { fileURLToPath } from 'url';
import _ from 'lodash'
// Import mongoose
import mongoose from 'mongoose';
// Import model and schema
import Course from './models/course.js';
dotenv.config();
const app = express();
app.use(cors())
app.get('/course', async (request, response) => {
try {
/**
* @type {{ [key: string]: { $regex: RegExp }}}
*/
const matchers = {};
// 從 request.query 取出 query parameter,
// 然後建構不分大小寫的 Regex 的查詢請求。
const addMatcher = (queryName) => {
if (queryName) {
var safeValue = _.escapeRegExp(request.query[queryName]);
matchers[queryName] = {
$regex: new RegExp(safeValue, 'i'),
};
}
};
for (const queryName of ['name', 'url', 'description', 'source']) {
addMatcher(queryName);
}
const matchedResult = await Course.find(matchers);
if (matchedResult?.length > 0) {
response.status(200);
} else {
response.status(400);
}
response.send({data: matchedResult});
} catch (err) {
response.status(500);
}
});
app.get('/course/:id', async (request, response) => {
const {id} = request.params;
const document = await Course.findById(id);
if (document) {
// 如果有內容,則回傳 200。
response.status(200);
} else {
// 如果沒有內容,則回傳 404。
response.status(404);
}
// 無論有沒有內容都回傳 data。
response.send({data: document});
});
app.get('/', (request, response) => {
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
response.status(200);
response.sendFile(__dirname + "/homepage/index.html");
});
async function main() {
const dbHost = process.env.MONGO_URL;
const port = process.env.PORT || '4000';
if (!dbHost) {
throw new Error('You must specify the MONGO_URL.');
}
// Connect to MongoDB
mongoose
.connect(dbHost, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log('MongoDB connection has been created.'));
const db = mongoose.connection;
db.on('error', (error) => console.log(error));
// Don't start Express before database started.
db.once('open', () => {
app.listen(port, () => console.log(`API started on port ${port}!`));
});
}
main();