server.js 942 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. });