-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
49 lines (35 loc) · 1.32 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
'use strict';
var express = require('express');
var cors = require('cors');
// ### User stories:
// 1. I can submit a form that includes a file upload.
// 2. The form file input field has the "name" attribute set to "upfile". We rely on this in testing.
// 3. When I submit something, I will receive the file name and size in bytes within the JSON response
// require and use "multer"...
var multer = require('multer');
var upload = multer({ dest: 'uploads/' });
var app = express();
app.use(cors());
app.use('/public', express.static(process.cwd() + '/public'));
app.get('/', function (req, res) {
res.sendFile(process.cwd() + '/views/index.html');
});
app.get('/hello', function(req, res){
res.json({greetings: "Hello, API"});
});
// POST Single File
// app.post('/api/fileanalyse', upload.single('upfile'), (req, res, next) => {
// res.send({name: req.file.originalname, type: req.file.mimetype, size: req.file.size});
// });
// POST multiple files
app.post('/api/fileanalyse', upload.array('upfile', 10), (req, res, next) => {
let mappedFiles = req.files.map(file => {
return {name: file.originalname, type: file.mimetype, size: file.size};
});
// console.log(mappedFiles)
res.send(mappedFiles);
// next();
});
app.listen(process.env.PORT || 3000, function () {
console.log('Node.js listening ...');
});