Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var path = require('path');
  2. var express = require('express');
  3. var app = express();
  4. var multer  = require('multer');
  5.  
  6. var storage = multer.diskStorage({
  7.   destination: function (req, file, cb) {
  8.     cb(null, './public/images/');
  9.   },
  10.   filename: function (req, file, cb) {
  11.     cb(null, Date.now() + file.originalname);
  12.   }
  13. });
  14.  
  15. var upload = multer({ storage: storage });
  16.  
  17. app.use(express.static(path.join(__dirname, 'public')));
  18.  
  19. app.post('/upload', upload.single('wallpaper'), function (req, res) {
  20.   var imagePath = req.file.path.replace(/^public\//, '');
  21.   res.redirect(imagePath);
  22. });
  23.  
  24. app.use(function (err, req, res, next) {
  25.   if (err instanceof multer.MulterError) res.status(500).send(err.message);
  26.   else next(err);
  27. });
  28.  
  29. app.listen(5000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement