-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
81 lines (56 loc) · 1.87 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import * as dotenv from 'dotenv';
dotenv.config();
import express from 'express';
import bodyParser from 'body-parser';
import mongoose from 'mongoose';
import cors from 'cors';
mongoose.set('strictQuery', true);
async function mongoConnection() {
// mongodb://${{ MONGOUSER }}:${{ MONGOPASSWORD }}@${{ MONGOHOST }}:${{ MONGOPORT }}
// const endpoint = 'mongodb+srv://sheriffderek:[email protected]/monsters-app';
const endpoint = 'mongodb://mongo:[email protected]:5616';
await mongoose.connect(endpoint);
}
mongoConnection().catch(err => console.log(err));
const monsterSchema = mongoose.Schema({
name: String,
color: String,
});
const Monster = mongoose.model('Monster', monsterSchema);
console.log('test.........');
// ========================
const app = express();
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use( cors() );
app.get('/', async function(request, response) {
const monsters = await Monster.find();
// Monster.findByIdAndDelete("639f87867b0a9c04714d1f2b");
console.log(monsters);
const form = `
<form action='/add' method='POST'>
<input type='text' name='name' />
<button type='submit'>Submit</button>
</form>
<a href='/'>Add</a>, <a href='monsters'>Monsters</a>
`;
response.send(form);
});
app.get('/monsters', function(request, response) {
Monster.find({}, function(error, monsters) {
response.send(monsters);
});
});
app.post('/add', async function(request, response) {
console.log(request.body);
const monster = new Monster(request.body); // { }
await monster.save();
response.redirect('/');
});
app.get('/posts', async function(request, response) {
const url = 'https://perpetual.education/wp-json/wp/v2/posts';
const data = await fetch(url);
const json = await data.json();
response.send(json);
});
app.listen(process.env.PORT);