-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.js
135 lines (116 loc) · 3.55 KB
/
app.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
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
131
132
133
134
135
const express = require('express');
const bodyParser = require('body-parser');
const morgan = require('morgan');
const mongoose = require('mongoose');
const routers = require('./server/routes');
const cors = require('cors');
const multer = require('multer');
const crypto = require('crypto');
const path = require('path');
const GridFsStorage = require('multer-gridfs-storage');
const Grid = require('gridfs-stream');
const Product = require('./server/models/productSchema');
const app = express();
// environment letiables for jwt token
require('dotenv').config();
mongoose.Promise = global.Promise;
const mongodb_uri = process.env.NODE_ENV ? process.env.MONGODB_URI : "mongodb://localhost/mydb";
mongoose.connect(mongodb_uri, { useNewUrlParser: true })
.catch(err => console.error(err));
// Setup mongodb connection for image storage
const conn = mongoose.connection;
let gfs;
conn.once('open', () => {
// Init Stream
gfs = Grid(conn.db, mongoose.mongo);
// Specifying name of the collection
gfs.collection('uploads');
});
// Middleware Setup
app.use(cors());
app.use(morgan('dev'));
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }));
app.use('/', routers);
// Custom middleware to handle errors
app.use((err, req, res, next) => {
console.log('Error:', err.message);
res.status(422).json(err.message);
});
const port = process.env.NODE_ENV ? process.env.PORT : 8000;
// Create Storage Engine
const storage = new GridFsStorage({
url: mongodb_uri,
file: (req, file) => {
return new Promise((resolve, reject) => {
crypto.randomBytes(16, (err, buf) => {
if (err) {
return reject(err);
}
const filename = buf.toString('hex') + path.extname(file.originalname);
const fileInfo = {
filename: filename,
bucketName: 'uploads' // this name should match the collection name
};
resolve(fileInfo);
});
});
}
});
const upload = multer({ storage });
// Route for image upload(s)
app.post('/uploaditem', upload.array('files'), (req, res) => {
const imageIsAvailable = req.files.length > 0;
const fileNames = [];
if (imageIsAvailable) {
const len = req.files.length;
for (let i = 0; i < len; i++) {
fileNames.push(req.files[i].filename);
}
}
const product = new Product({
...req.body,
imageIsAvailable,
fileNames
});
product.save().then(() => {
res.send('ok');
});
});
// Route for getting image
app.get('/image/:filename', (req, res) => {
gfs.files.findOne({ filename: req.params.filename }, (err, file) => {
// Check if file exists
if (!file) {
return res.status(404).json({
err: 'No file exists'
});
}
//Check if an image
if (file.contentType === 'image/jpeg' || file.contentType === 'image/png' || file.contentType === 'image/jpg' || file.contentType) {
const readStream = gfs.createReadStream(file.filename);
readStream.pipe(res);
} else {
res.status(404).json({
err: 'Not an image'
});
}
});
});
// @route: for removing item from database
app.post('/removeitem', (req, res) => {
const item = req.body;
for (let i = 0; i < item.fileNames.length; i++) {
gfs.remove({
filename: item.fileNames[i],
root: 'uploads' // Important to specify collection name (Not given in docs)
}, function (err, gridStore) {
if (err) throw err;
});
}
Product.deleteOne({ _id: item._id }).then((result) => {
res.send('item deleted');
});
});
app.listen(process.env.PORT || 8000);
console.log(`Listening to port ${port}`);