Reputation: 479
I found this sample code of a nodeJS server. I don't understand what this line of code does, what is the true for:
var urlParts = url.parse(req.url, true)
The following line is also unclear to me. Why is it neccessairy to write data.txt?
fs.writeFileSync("data.txt", data);
SERVER
server = http.createServer(function(req, res) {
res.writeHead(200, {
'Content-Type': 'text/plain',
'Access-Control-Allow-Origin': 'http://localhost'
});
var urlParts = url.parse(req.url, true),
data = urlParts.query.data;
fs.writeFileSync("data.txt", data);
res.end("OK");
});
Upvotes: 0
Views: 3439
Reputation: 13159
This line require('url').parse(req.url, true)
return URL object (see bottom), and pass true as the second argument to also parse the query.
See : http://nodejs.org/docs/latest/api/url.html
Upvotes: 6