Skip to content

Commit

Permalink
Updated: new backend demo & fix ng2-bs version
Browse files Browse the repository at this point in the history
  • Loading branch information
buchslava committed Oct 16, 2015
1 parent 494b9f9 commit ed1c9e6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
52 changes: 33 additions & 19 deletions demo/components/file-upload/file-catcher.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,49 @@
var express = require('express');
var Busboy = require('busboy');
var path = require('path');
var multer = require('multer');
var fs = require('fs');

var app = express();

var DIR = './uploads/';

var upload = multer({dest: DIR});

app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '!put your host here!');
res.setHeader('Access-Control-Allow-Origin', 'http://valor-software.github.io');
res.setHeader('Access-Control-Allow-Methods', 'POST');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});

app.use(multer({
dest: DIR,
rename: function (fieldname, filename) {
return filename + Date.now();
},
onFileUploadStart: function (file) {
console.log(file.originalname + ' is starting ...');
},
onFileUploadComplete: function (file) {
console.log(file.fieldname + ' uploaded to ' + file.path);
}
}));

app.get('/api', function (req, res) {
res.end('file catcher example');
});

app.post('/api', function (req, res) {
var fstream;
var files = [];
var busboy = new Busboy({headers: req.headers});
busboy.on('file', function (fieldname, file, filename) {
fstream = fs.createWriteStream(__dirname + '/uploads/' + filename);
file.pipe(fstream);
fstream.on('close', function () {
files.push(filename);
file.resume();
});
});
upload(req, res, function (err) {
if (err) {
return res.end(err.toString());
}

busboy.on('finish', function () {
res.end('ok');
res.end('File is uploaded');
});
req.pipe(busboy);
});

app.listen(process.env.PORT || 3000);
var PORT = process.env.PORT || 3000;

app.listen(PORT, function () {
console.log('Working on port ' + PORT);
});
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
},
"homepage": "https://github.com/valor-software/ng2-file-upload#readme",
"dependencies": {
"angular2": "^2.0.0-alpha.42",
"ng2-bootstrap": "^0.40.0",
"angular2": "2.0.0-alpha.42",
"ng2-bootstrap": "0.42.0",
"reflect-metadata": "0.1.2",
"ts-loader": "0.5.6",
"zone.js": "0.5.8"
Expand Down
1 change: 1 addition & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ var config = {
loader: 'ts',
query: {
ignoreDiagnostics: [
6053,
// TS2305 -> Module 'ng' has no exported member
2305,
// TS2307 -> Cannot find external module
Expand Down

0 comments on commit ed1c9e6

Please sign in to comment.