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.

41 lines
942 B

  1. const express = require('express');
  2. const fileUpload = require('../lib/index');
  3. const app = express();
  4. const PORT = 8000;
  5. app.use('/form', express.static(__dirname + '/index.html'));
  6. // default options
  7. app.use(fileUpload());
  8. app.get('/ping', function(req, res) {
  9. res.send('pong');
  10. });
  11. app.post('/upload', function(req, res) {
  12. let sampleFile;
  13. let uploadPath;
  14. if (!req.files || Object.keys(req.files).length === 0) {
  15. res.status(400).send('No files were uploaded.');
  16. return;
  17. }
  18. console.log('req.files >>>', req.files); // eslint-disable-line
  19. sampleFile = req.files.sampleFile;
  20. uploadPath = __dirname + '/uploads/' + sampleFile.name;
  21. sampleFile.mv(uploadPath, function(err) {
  22. if (err) {
  23. return res.status(500).send(err);
  24. }
  25. res.send('File uploaded to ' + uploadPath);
  26. });
  27. });
  28. app.listen(PORT, function() {
  29. console.log('Express server listening on port ', PORT); // eslint-disable-line
  30. });