summaryrefslogtreecommitdiffstats
path: root/app.js
blob: ecbf5985444ef966ce534b45b332b594007588fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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);
});