Created
May 6, 2018 03:51
-
-
Save budasuyasa/7666b2fa1f0b9db41fe2b9b100221295 to your computer and use it in GitHub Desktop.
medium-post-simple-rest-04
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const bodyParser = require('body-parser'); //post body handler | |
const { check, validationResult } = require('express-validator/check'); //form validation | |
const { matchedData, sanitize } = require('express-validator/filter'); //sanitize form params | |
const multer = require('multer'); //multipar form-data | |
const path = require('path'); | |
const crypto = require('crypto'); | |
//Set body parser for HTTP post operation | |
app.use(bodyParser.json()); // support json encoded bodies | |
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies | |
//set static assets to public directory | |
app.use(express.static('public')); | |
const uploadDir = '/img/'; | |
const storage = multer.diskStorage({ | |
destination: "./public"+uploadDir, | |
filename: function (req, file, cb) { | |
crypto.pseudoRandomBytes(16, function (err, raw) { | |
if (err) return cb(err) | |
cb(null, raw.toString('hex') + path.extname(file.originalname)) | |
}) | |
} | |
}) | |
const upload = multer({storage: storage, dest: uploadDir }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment