I have a buffer in node.js and I'm checking for mime type with regex.
There is a capturing group in regex and if it is successfull it must return this capturing group at index 1 in the array returned by exec.
I'm using
if(mime.exec(dt)[1]){
tip.push(mime.exec(dt)[1]);
}
this control and I also tried
if(1 in mime.exec)
and also
mime.exec.hasOwnProperty(1)
but anyway the condition is processed and gives traceback
TypeError: Cannot read property '1' of null
What kind of mechanism can I use to fix this issue?
UPDATE ----
var mime=/^content-type: (.+\S)/igm;
UPDATE ----
var fs = require("fs"),
mime = /^content-type: (.+\S)/igm,
tip = [];
require("http").createServer(function(req, res) {
var data = "";
console.log("working...");
console.log(req.method);
if (req.method.toUpperCase() == "POST") {
req.once("data", function() {
fs.writeFileSync("dene.txt", "");
});
req.on("data", function(dt) {
fs.appendFileSync("dene.txt", dt.toString("utf8"));
if (mime.exec(dt)[1]) {
tip.push(mime.exec(dt)[1]);
} else {
return false;
}
});
req.on("end", function() {
console.log(((fs.statSync("dene.txt").size) / 1024).toFixed(2), "kb");
console.log(tip);
});
}
res.writeHead(200, {
"content-type": "text/html"
});
res.end(require("fs").readFileSync(require("path").resolve(__dirname, "static_files/post.html")));
}).listen(3000)