Skip to content

Commit 9c1f1c9

Browse files
committed
Updated flow.js node backend example.
The "Access-Control-Allow-Origin" headers are easily configurable now.
1 parent 47f8273 commit 9c1f1c9

1 file changed

Lines changed: 31 additions & 20 deletions

File tree

samples/Node.js/app.js

Lines changed: 31 additions & 20 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-
});
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+
'Access-Control-Allow-Origin': '*'
2125
});
26+
});
2227
});
2328

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

3538
// Handle status checks on chunks through Flow.js
3639
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-
});
40+
flow.get(req, function(status, filename, original_filename, identifier) {
41+
console.log('GET', status);
42+
res.header("Access-Control-Allow-Origin", "*");
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)