Skip to content

Instantly share code, notes, and snippets.

@EeroHeroHeino
Created October 25, 2016 04:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EeroHeroHeino/6aa3f8e89c9a123e74658a4bf7152f1c to your computer and use it in GitHub Desktop.
Save EeroHeroHeino/6aa3f8e89c9a123e74658a4bf7152f1c to your computer and use it in GitHub Desktop.
var express = require('express')
, cors = require('cors')
, app = express();
var fs = require('fs');
app.use(cors());
app.post('/post', function(req, res, next){
var body = [];
req.on('data', function(chunk) {
body.push(chunk);
}).on('end', function() {
body = Buffer.concat(body).toString();
console.log(body);
var parsedData = JSON.parse(body);
var filename = parsedData['q'].replace(' ','_').replace(/\./g,'').replace(/\\/g,'');
var filepath = "/tmp/"+filename+'.txt';
fs.writeFile(filepath, body, function(err) {
if(err) {
return console.log(err);
}
console.log("The file "+filepath+" was saved!");
});
res.json({msg: 'This is CORS-enabled for all origins!'});
});
});
app.listen(8000, function(){
console.log('CORS-enabled web server listening on port 80');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment