-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
219 lines (155 loc) · 4.12 KB
/
server.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//express
const express = require('express');
//mongoose
const mongoose = require('mongoose');
//methodOverride
const methodOverride = require('method-override');
//multer
const multer = require('multer');
//gfs
const GridFsStorage = require('multer-gridfs-storage');
//
const Grid = require('gridfs-stream');
//path
const path = require('path');
//
const bodyParser = require('body-parser');
const crypto = require('crypto');
//calling
var app = express();
var port_no = 3000;
//ejs for views
app.set('view engine','ejs');
var db_uri = 'mongodb://testBooks:[email protected]:13845/book_rest_api';
//mongoose url
mongoose.connect(db_uri);
//Middleware for the bodyParser
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());
//
app.use(methodOverride('_method'))
//mongoose connection
var conn = mongoose.connection;
//db err
conn.on('err',function (err)
{
console.log('err on db'+err);
});
let gfs;
//db connect
conn.once('open',function () {
//init gridfs-stream github
gfs = Grid(conn.db, mongoose.mongo);
gfs.collection('uploads');
// all set!
});
//create storage engine multer-gridfs-storage github
const storage = new GridFsStorage({
url: db_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'
};
resolve(fileInfo);
});
});
}
});
const upload = multer({ storage });
//get method
app.get('/',function (request,response)
{
response.render('index');
});
//post method
app.post('/upload', upload.single('uploadfile'),function (request,response)
{
//response.json({file:request.file});
response.redirect('/');
});
// @route GET /files
// @desc Display all files in JSON
app.get('/files',function (request,response)
{
gfs.files.find().toArray(function (err,files)
{
if(err)
{
console.log(err);
response.status(404).json(err);
}
else {
console.log(files);
response.send(files);
}
})
});
//get by filename
app.get('/files/:filename', function(request,response)
{
console.log('filename By Name'+request.params.filename);
gfs.files.findOne({filename: request.params.filename},function (err,file)
{
if(err)
{
console.log('err'+err);
response.status(404).send(err);
}
else {
response.send(file);
}
})
})
//get by filename github gridfs-stream topic createreadstream
app.get('/image/:filename', function(request,response)
{
console.log('filename By Name'+request.params.filename);
gfs.files.findOne({filename: request.params.filename},function (err,file)
{
if(err || (!file || file.length ===0 ))
{
console.log('err'+err);
response.status(404).send(err);
}
else
{
//check if image
if(file.contentType === 'image/jpeg'|| file.contentType === 'img/png'|| file.contentType === 'text/plain')
{
//response.send(file);
var readstream = gfs.createReadStream(file.filename);
readstream.pipe(response);
}
else {
res.status(404).send(err);
}
}
})
})
//delete file By id
//add root:'uploads'
app.delete('/files/del/:id',function (request,response)
{
console.log('delete By filename'+request.params.id);
gfs.remove({_id:request.params.id,root:'uploads'},function (err, files)
{
if(err)
{
response.send(err);
}
else {
response.redirect('/');
}
});
});
app.listen(process.env.port||port_no);
console.log('server listen to the port'+port_no);