Created
October 25, 2016 04:57
-
-
Save EeroHeroHeino/6aa3f8e89c9a123e74658a4bf7152f1c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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