-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
40 lines (31 loc) · 1.04 KB
/
index.js
File metadata and controls
40 lines (31 loc) · 1.04 KB
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
import express from 'express';
import mongoose from 'mongoose';
import router from './routes/problems.js';
try {
const app = express();
await mongoose.connect(`mongodb://127.0.0.1:27017/${process.env.DB_NAME}`, {
serverSelectionTimeoutMS: 3000
});
//Middleware to support application/json
app.use(express.json());
//Middleware to support
app.use(express.urlencoded({extended: true}));
app.use((req, res, next) => {
// Block non accept: application/json requests
if (req.header('Accept') !== 'application/json' && req.method !== 'OPTIONS'){
res.status(406);
res.json({error: 'only JSON is allowed as Accept header (Math Problems API)'});
return;
}
next();
});
app.get('/', (req, res) => {
res.json({message: 'Welcome to my webservice for math problems!'});
});
app.use('/problems', router);
app.listen(process.env.EXPRESS_PORT, () => {
console.log('Server has started');
});
} catch (e) {
console.log('e');
};