You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
942 B
42 lines
942 B
4 years ago
|
const express = require('express');
|
||
|
const fileUpload = require('../lib/index');
|
||
|
const app = express();
|
||
|
|
||
|
const PORT = 8000;
|
||
|
app.use('/form', express.static(__dirname + '/index.html'));
|
||
|
|
||
|
// default options
|
||
|
app.use(fileUpload());
|
||
|
|
||
|
app.get('/ping', function(req, res) {
|
||
|
res.send('pong');
|
||
|
});
|
||
|
|
||
|
app.post('/upload', function(req, res) {
|
||
|
let sampleFile;
|
||
|
let uploadPath;
|
||
|
|
||
|
if (!req.files || Object.keys(req.files).length === 0) {
|
||
|
res.status(400).send('No files were uploaded.');
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
console.log('req.files >>>', req.files); // eslint-disable-line
|
||
|
|
||
|
sampleFile = req.files.sampleFile;
|
||
|
|
||
|
uploadPath = __dirname + '/uploads/' + sampleFile.name;
|
||
|
|
||
|
sampleFile.mv(uploadPath, function(err) {
|
||
|
if (err) {
|
||
|
return res.status(500).send(err);
|
||
|
}
|
||
|
|
||
|
res.send('File uploaded to ' + uploadPath);
|
||
|
});
|
||
|
});
|
||
|
|
||
|
app.listen(PORT, function() {
|
||
|
console.log('Express server listening on port ', PORT); // eslint-disable-line
|
||
|
});
|