-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.mjs
130 lines (88 loc) · 3.21 KB
/
app.mjs
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import express from 'express';
import path from 'path';
import {fileURLToPath} from 'url';
import cors from 'cors';
import multer from 'multer';
import {list_plants} from "./controllers/plants.mjs";
import {plants} from "./routes/plants.mjs";
const app = express();
app.use(cors());
app.use(express.urlencoded({extended: true}));
app.use(express.json());
app.set('view engine', 'pug');
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
app.use('/static', express.static(path.join(__dirname, 'public')));
app.get('/', async (req, res) => {
const url = 'http://localhost:3001/api/plants';
const response = await fetch(url);
const json = await response.json();
res.locals.plants = json;
// console.log(json);
res.status(200).render('home');
});
app.get('/plants/:id', async (req, res, next) =>{
const url = `http://localhost:3001/api/plants/${req.params.id}`;
console.log(url)
const response = await fetch(url);
const json = await response.json();
// console.log(": " + json)
res.locals.plants = json;
// console.log("name: " + res.locals.name);
res.status(200).render('plant')
})
app.get('/nursery', async (req, res) => {
const url = 'http://localhost:3001/api/plants';
const data = await list_plants();
JSON.stringify(data);
const response = await fetch(url);
const json = await response.json();
res.locals.plants = json;
res.locals.data = data;
console.log(res.locals);
res.status(200).render('nursery');
});
app.get('/graveyard', async (req, res) => {
const url = 'http://localhost:3001/api/plants';
const response = await fetch(url);
const json = await response.json();
res.locals.plants = json;
console.log(res.locals);
res.status(200).render('graveyard');
});
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './public/images')
},
filename: function (req, file, cb) {
cb(null, req.body.picname + ".jpg")
}
})
var upload = multer({ storage: storage })
app.use('/uploads', express.static('uploads'));
app.post('/', upload.single('profile-file', 'picname'), async function (req, res, next) {
// req.file is the `profile-file` file
// req.body will hold the text fields, if there were any
console.log("the rename name" + JSON.stringify(req.body.picname))
console.log("file" + JSON.stringify(req.file))
const url = 'http://localhost:3001/api/plants';
const response = await fetch(url);
const json = await response.json();
res.locals.plants = json;
console.log(res.locals);
// res.status(200).render('home');
res.redirect(303, '/');
})
app.post('/nursery', upload.single('profile-file', 'picname'), async function (req, res, next) {
// req.file is the `profile-file` file
// req.body will hold the text fields, if there were any
console.log(JSON.stringify(req.body.picname))
console.log(JSON.stringify(req.file))
const url = 'http://localhost:3001/api/nursery';
const response = await fetch(url);
const json = await response.json();
res.locals.plants = json;
console.log(res.locals);
res.status(200).render('nursery');
})
export {app}