Skip to content

Commit 81ef017

Browse files
committed
Merge pull request #51 from Partyschaum/fix-node-example-app
fix(sample): updated flow.js node backend
2 parents 3abc0bf + b96cd40 commit 81ef017

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

samples/Node.js/app.js

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,53 @@ var multipartMiddleware = multipart();
66
var flow = require('./flow-node.js')('tmp');
77
var app = express();
88

9+
// Configure access control allow origin header stuff
10+
var ACCESS_CONTROLL_ALLOW_ORIGIN = false;
11+
912
// Host most stuff in the public folder
1013
app.use(express.static(__dirname + '/public'));
1114
app.use(express.static(__dirname + '/../../src'));
1215

1316
// Handle uploads through Flow.js
1417
app.post('/upload', multipartMiddleware, function(req, res) {
15-
flow.post(req, function(status, filename, original_filename, identifier) {
16-
console.log('POST', status, original_filename, identifier);
17-
res.send(200, {
18-
// NOTE: Uncomment this funciton to enable cross-domain request.
19-
//'Access-Control-Allow-Origin': '*'
20-
});
21-
});
18+
flow.post(req, function(status, filename, original_filename, identifier) {
19+
console.log('POST', status, original_filename, identifier);
20+
if (ACCESS_CONTROLL_ALLOW_ORIGIN) {
21+
res.header("Access-Control-Allow-Origin", "*");
22+
}
23+
res.status(status).send();
24+
});
2225
});
2326

24-
// Handle cross-domain requests
25-
// NOTE: Uncomment this funciton to enable cross-domain request.
26-
/*
27-
app.options('/upload', function(req, res){
27+
28+
app.options('/upload', function(req, res){
2829
console.log('OPTIONS');
29-
res.send(true, {
30-
'Access-Control-Allow-Origin': '*'
31-
}, 200);
32-
});
33-
*/
30+
if (ACCESS_CONTROLL_ALLOW_ORIGIN) {
31+
res.header("Access-Control-Allow-Origin", "*");
32+
}
33+
res.status(200).send();
34+
});
3435

3536
// Handle status checks on chunks through Flow.js
3637
app.get('/upload', function(req, res) {
37-
flow.get(req, function(status, filename, original_filename, identifier) {
38-
console.log('GET', status);
39-
res.send(status == 'found' ? 200 : 404);
40-
});
38+
flow.get(req, function(status, filename, original_filename, identifier) {
39+
console.log('GET', status);
40+
if (ACCESS_CONTROLL_ALLOW_ORIGIN) {
41+
res.header("Access-Control-Allow-Origin", "*");
42+
}
43+
44+
if (status == 'found') {
45+
status = 200;
46+
} else {
47+
status = 404;
48+
}
49+
50+
res.status(status).send();
51+
});
4152
});
4253

4354
app.get('/download/:identifier', function(req, res) {
44-
flow.write(req.params.identifier, res);
55+
flow.write(req.params.identifier, res);
4556
});
4657

4758
app.listen(3000);

0 commit comments

Comments
 (0)