Created
April 7, 2017 18:28
-
-
Save dmamills/83d570bf62c8e7db0cd065713a10286f to your computer and use it in GitHub Desktop.
This file contains hidden or 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 express = require('express'); | |
const bodyParser = require('body-parser'); | |
const cors = require('cors'); | |
const logger = require('morgan'); | |
const multipart = require('connect-multiparty'); | |
const fs = require('fs'); | |
const shortid = require('shortid'); | |
let app = express(); | |
let multipartMiddleware = multipart(); | |
app.use(cors()); | |
app.use(logger("dev")); | |
app.use(bodyParser.json({limit: '50mb'})); | |
app.use(bodyParser.urlencoded({limit: '50mb', extended: true})); | |
app.use(express.static('public')); | |
app.post('/upload', multipartMiddleware, (req, res) => { | |
let files = req.files; | |
let img = files.image; | |
let oldPath = img.path; | |
let extension = img.headers['content-type'].split('/')[1]; | |
let name = shortid.generate(); | |
let newPath = __dirname + '/public/' + name + '.' + extension; | |
fs.rename(oldPath, newPath, (err) => { | |
if(err) throw err; | |
let publicUrl = 'http://localhost:9000/'+ name + '.' + extension; | |
res.json({ | |
publicUrl | |
}); | |
}); | |
}); | |
app.listen(9000); | |
console.log('Listening on port 9000'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment