Skip to content

Instantly share code, notes, and snippets.

@ssshake
Forked from dmamills/index.js
Created April 7, 2017 18:29
Show Gist options
  • Save ssshake/216d51223df91552b89d7cac303a1df4 to your computer and use it in GitHub Desktop.
Save ssshake/216d51223df91552b89d7cac303a1df4 to your computer and use it in GitHub Desktop.
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