diff options
author | Kyle K <kylek389@gmail.com> | 2017-03-19 02:20:34 -0500 |
---|---|---|
committer | Kyle K <kylek389@gmail.com> | 2017-03-19 02:20:34 -0500 |
commit | f16ed31e84dc8fda7f217be12dd715690b54f471 (patch) | |
tree | c483f4eaeced3ef5bcdedb2e6549348e2c6cf99b /app.js | |
parent | 00df5136aaf9a2aee9b9b5121f73dee7f3810d29 (diff) | |
download | express-upload-f16ed31e84dc8fda7f217be12dd715690b54f471.tar.gz express-upload-f16ed31e84dc8fda7f217be12dd715690b54f471.tar.bz2 express-upload-f16ed31e84dc8fda7f217be12dd715690b54f471.zip |
- add gulpfile and bower preparing for use with vue.js and jquery
- use express 4.x
Diffstat (limited to 'app.js')
-rw-r--r-- | app.js | 40 |
1 files changed, 40 insertions, 0 deletions
@@ -0,0 +1,40 @@ +var express = require('express'), + app = express(), + multer = require('multer'); +var fs = require('fs'); + +var imgs = ['png', 'jpg', 'jpeg', 'gif', 'bmp']; // only make thumbnail for these + +function getExtension(fn) { + return fn.split('.').pop(); +} + +function fnAppend(fn, insert) { + var arr = fn.split('.'); + var ext = arr.pop(); + insert = (insert !== undefined) ? insert : new Date().getTime(); + return arr + '.' + insert + '.' + ext; +} + + +app.use(multer({ + dest: './static/uploads/', + rename: function (fieldname, filename) { + return filename.replace(/\W+/g, '-').toLowerCase(); + } +})); +app.use(express.static(__dirname + '/static')); + + +app.post('/api/upload', function (req, res) { + res.send({image: false, file: req.files.userFile.originalname, savedAs: req.files.userFile.name}); +}); +app.get('/api/filenames', function (req, res) { // this is the RESTful API that will send json reply to browser with filenames list + var fnames = fs.readdir('./static/uploads', function (err, files) { + res.send(JSON.stringify(files)); + }); +}); + +var server = app.listen(8081, function () { + console.log('listening on port %d', server.address().port); +}); |