-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
64 lines (54 loc) · 1.74 KB
/
app.js
File metadata and controls
64 lines (54 loc) · 1.74 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import express from 'express';
import dotenv from 'dotenv';
import ReflectionWithJsObject from './src/usingJSObject/controllers/reflection';
import ReflectionWithDB from './src/usingDB/controllers/reflection';
import { multerUploads, dataUri } from './src/usingDB/multer';
import {
uploader,
cloudinaryConfig
} from './src/usingDB/config/cloudinaryConfig';
dotenv.config();
const Reflection =
process.env.TYPE === 'db' ? ReflectionWithDB : ReflectionWithJsObject;
const app = express();
console.log(` NODE_TYPE: ${process.env.TYPE} `);
console.log(`app: ${app.get('env')}`);
if (app.get('env') === 'development') {
console.log('Morgan runing');
}
app.use(express.json());
app.use('*', cloudinaryConfig);
const port = process.env.PORT || 3380;
app.get('/', (req, res) => {
res.status(200).send({ message: 'hello worldn MY jamiu talking' });
});
app.post('/upload', multerUploads, (req, res) => {
if (req.file) {
const file = dataUri(req).content;
return uploader
.upload(file)
.then(result => {
const image = result.url;
return res.status(200).json({
messge: 'Your image has been uploded successfully to cloudinary',
data: {
image
}
});
})
.catch(err =>
res.status(400).json({
messge: 'someting went wrong while processing your request',
data: {
err
}
})
);
}
});
app.post('/api/v1/reflections', Reflection.create);
app.get('/api/v1/reflections', Reflection.getAll);
app.get('/api/v1/reflections/:id', Reflection.getOne);
app.put('/api/v1/reflections/:id', Reflection.update);
app.delete('/api/v1/reflections:id', Reflection.delete);
app.listen(port, () => console.log(`listening at ${port}`));