Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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