Comparing version 1.2.17 to 1.2.18
346
lib/Grand.js
@@ -31,2 +31,5 @@ /* | ||
this.postRouters = {}; | ||
this.deleteRouters = {}; | ||
this.patchRouters = {}; | ||
this.putRouters = {}; | ||
this.classes = []; | ||
@@ -47,2 +50,5 @@ this.bases = []; | ||
this.postRouters = []; | ||
this.deleteRouters = []; | ||
this.putRouters = []; | ||
this.patchRouters = []; | ||
this.staticFolder = options.staticFolder || self.staticFolder; | ||
@@ -57,2 +63,5 @@ this.id = Math.random() | ||
this.initedposts = false; | ||
this.initedputs = false; | ||
this.initedpatches = false; | ||
this.initeddeletes = false; | ||
} | ||
@@ -93,8 +102,11 @@ serveFiles(cb) { | ||
} else if ( | ||
method == "post" || | ||
method == "patch" || | ||
method == "put" || | ||
method == "delete" | ||
method == "post" | ||
) { | ||
this.postRouters.push(setOptions); | ||
} else if (method == "delete") { | ||
this.deleteRouters.push(setOptions) | ||
} else if (method == "patch") { | ||
this.patchRouters.push(setOptions); | ||
} else if (method == "put") { | ||
this.putRouters.push(setOptions); | ||
} | ||
@@ -185,6 +197,3 @@ } | ||
} else if ( | ||
req.method.toLowerCase() == "post" || | ||
req.method.toLowerCase() == "put" || | ||
req.method.toLowerCase() == "delete" || | ||
req.method.toLowerCase() == "patch" | ||
req.method.toLowerCase() == "post" | ||
) { | ||
@@ -235,6 +244,3 @@ let postRouters = this.postRouters; | ||
if ( | ||
matchedURL.method.toLowerCase() == "post" || | ||
matchedURL.method.toLowerCase() == "put" || | ||
matchedURL.method.toLowerCase() == "delete" || | ||
matchedURL.method.toLowerCase() == "patch" | ||
matchedURL.method.toLowerCase() == "post" | ||
) { | ||
@@ -297,2 +303,318 @@ res.statusCode = 200; | ||
} | ||
} else if ( | ||
req.method.toLowerCase() == "delete" | ||
) { | ||
let deleteRouters = this.deleteRouters; | ||
if (this.initeddeletes == false) { | ||
this.finalDeleteRouters = []; | ||
this.deleteRouters.forEach(router => { | ||
router.url = `${this.base}${router.url}` | ||
.replace(/(https?:\/\/)|(\/)+/g, "$1$2") | ||
.replace(/^(.+?)\/*?$/, "$1"); | ||
router.method = router.method.toLowerCase(); | ||
let routePattern = new RouteParser(router.url); | ||
routePattern.class = this; | ||
let values = []; | ||
routePattern.ast.map(tag => { | ||
values.push(tag.value); | ||
}); | ||
values = values.join(""); | ||
router.value = values | ||
.replace(/(https?:\/\/)|(\/)+/g, "$1$2") | ||
.replace(/^(.+?)\/*?$/, "$1"); | ||
this.finalDeleteRouters.push(routePattern); | ||
}); | ||
this.initeddeletes = true; | ||
} | ||
self.deleteRouters[this.id] = deleteRouters; | ||
let matchedFinal; | ||
let matchedRe = req.pathname; | ||
// console.log(this.finalDeleteRouters); | ||
this.finalDeleteRouters.forEach(r => { | ||
matchedFinal = r.match(req.pathname); | ||
if (matchedFinal) { | ||
let values = []; | ||
r.ast.map(tag => { | ||
values.push(tag.value); | ||
}); | ||
values = values.join(""); | ||
matchedRe = values | ||
.replace(/(https?:\/\/)|(\/)+/g, "$1$2") | ||
.replace(/^(.+?)\/*?$/, "$1"); | ||
req.params = matchedFinal; | ||
} | ||
}); | ||
let matchedURL = deleteRouters.find(router => { | ||
return router.value == matchedRe; | ||
}); | ||
if (matchedURL) { | ||
if ( | ||
matchedURL.method.toLowerCase() == "delete" | ||
) { | ||
res.statusCode = 200; | ||
let headers = req.headers; | ||
let comingData = ""; | ||
req.on("data", function(data) { | ||
comingData += data; | ||
}); | ||
req.on("end", function() { | ||
if (comingData.length > 0) { | ||
if ( | ||
headers["content-type"] === | ||
"application/x-www-form-urlencoded" | ||
) { | ||
comingData = querystring.parse(comingData); | ||
} else if (headers["content-type"] === "application/json") { | ||
comingData = JSON.parse(comingData); | ||
} else if ( | ||
headers["content-type"] === "text/plain" || | ||
headers["content-type"] === "text/plain;charset=UTF-8" | ||
) { | ||
try { | ||
comingData = JSON.parse(comingData); | ||
} catch (e) { | ||
comingData = comingData; | ||
} | ||
} | ||
req.data = comingData; | ||
} | ||
let middleWares = matchedURL.middleWares; | ||
if (middleWares && middleWares.length > 0) { | ||
var nexted = 0; | ||
middleWares[0](req, res, next); | ||
function next() { | ||
nexted++; | ||
if (middleWares.length <= nexted) { | ||
return matchedURL.handler(req, res); | ||
} else { | ||
return middleWares[nexted](req, res, next); | ||
} | ||
} | ||
} else { | ||
return matchedURL.handler(req, res); | ||
} | ||
}); | ||
} else { | ||
res.writeHead(404); | ||
return this.errorPage ? | ||
this.errorPage(req, res) : | ||
self.errorPage(req, res); | ||
} | ||
} else { | ||
res.writeHead(404); | ||
return this.errorPage ? | ||
this.errorPage(req, res) : | ||
self.errorPage(req, res); | ||
} | ||
} else if ( | ||
req.method.toLowerCase() == "put" | ||
) { | ||
let putRouters = this.putRouters; | ||
if (this.initedputs == false) { | ||
this.finalPutRouters = []; | ||
this.putRouters.forEach(router => { | ||
router.url = `${this.base}${router.url}` | ||
.replace(/(https?:\/\/)|(\/)+/g, "$1$2") | ||
.replace(/^(.+?)\/*?$/, "$1"); | ||
router.method = router.method.toLowerCase(); | ||
let routePattern = new RouteParser(router.url); | ||
routePattern.class = this; | ||
let values = []; | ||
routePattern.ast.map(tag => { | ||
values.push(tag.value); | ||
}); | ||
values = values.join(""); | ||
router.value = values | ||
.replace(/(https?:\/\/)|(\/)+/g, "$1$2") | ||
.replace(/^(.+?)\/*?$/, "$1"); | ||
this.finalPutRouters.push(routePattern); | ||
}); | ||
this.initedputs = true; | ||
} | ||
self.putRouters[this.id] = putRouters; | ||
let matchedFinal; | ||
let matchedRe = req.pathname; | ||
this.finalPutRouters.forEach(r => { | ||
matchedFinal = r.match(req.pathname); | ||
if (matchedFinal) { | ||
let values = []; | ||
r.ast.map(tag => { | ||
values.push(tag.value); | ||
}); | ||
values = values.join(""); | ||
matchedRe = values | ||
.replace(/(https?:\/\/)|(\/)+/g, "$1$2") | ||
.replace(/^(.+?)\/*?$/, "$1"); | ||
req.params = matchedFinal; | ||
} | ||
}); | ||
let matchedURL = putRouters.find(router => { | ||
return router.value == matchedRe; | ||
}); | ||
if (matchedURL) { | ||
if ( | ||
matchedURL.method.toLowerCase() == "delete" | ||
) { | ||
res.statusCode = 200; | ||
let headers = req.headers; | ||
let comingData = ""; | ||
req.on("data", function(data) { | ||
comingData += data; | ||
}); | ||
req.on("end", function() { | ||
if (comingData.length > 0) { | ||
if ( | ||
headers["content-type"] === | ||
"application/x-www-form-urlencoded" | ||
) { | ||
comingData = querystring.parse(comingData); | ||
} else if (headers["content-type"] === "application/json") { | ||
comingData = JSON.parse(comingData); | ||
} else if ( | ||
headers["content-type"] === "text/plain" || | ||
headers["content-type"] === "text/plain;charset=UTF-8" | ||
) { | ||
try { | ||
comingData = JSON.parse(comingData); | ||
} catch (e) { | ||
comingData = comingData; | ||
} | ||
} | ||
req.data = comingData; | ||
} | ||
let middleWares = matchedURL.middleWares; | ||
if (middleWares && middleWares.length > 0) { | ||
var nexted = 0; | ||
middleWares[0](req, res, next); | ||
function next() { | ||
nexted++; | ||
if (middleWares.length <= nexted) { | ||
return matchedURL.handler(req, res); | ||
} else { | ||
return middleWares[nexted](req, res, next); | ||
} | ||
} | ||
} else { | ||
return matchedURL.handler(req, res); | ||
} | ||
}); | ||
} else { | ||
res.writeHead(404); | ||
return this.errorPage ? | ||
this.errorPage(req, res) : | ||
self.errorPage(req, res); | ||
} | ||
} else { | ||
res.writeHead(404); | ||
return this.errorPage ? | ||
this.errorPage(req, res) : | ||
self.errorPage(req, res); | ||
} | ||
} else if ( | ||
req.method.toLowerCase() == "patch" | ||
) { | ||
let patchRouters = this.patchRouters; | ||
if (this.initedpatches == false) { | ||
this.finalPatchRouters = []; | ||
this.patchRouters.forEach(router => { | ||
router.url = `${this.base}${router.url}` | ||
.replace(/(https?:\/\/)|(\/)+/g, "$1$2") | ||
.replace(/^(.+?)\/*?$/, "$1"); | ||
router.method = router.method.toLowerCase(); | ||
let routePattern = new RouteParser(router.url); | ||
routePattern.class = this; | ||
let values = []; | ||
routePattern.ast.map(tag => { | ||
values.push(tag.value); | ||
}); | ||
values = values.join(""); | ||
router.value = values | ||
.replace(/(https?:\/\/)|(\/)+/g, "$1$2") | ||
.replace(/^(.+?)\/*?$/, "$1"); | ||
this.finalPatchRouters.push(routePattern); | ||
}); | ||
this.initedpatches = true; | ||
} | ||
self.patchRouters[this.id] = patchRouters; | ||
let matchedFinal; | ||
let matchedRe = req.pathname; | ||
this.finalPatchRouters.forEach(r => { | ||
matchedFinal = r.match(req.pathname); | ||
if (matchedFinal) { | ||
let values = []; | ||
r.ast.map(tag => { | ||
values.push(tag.value); | ||
}); | ||
values = values.join(""); | ||
matchedRe = values | ||
.replace(/(https?:\/\/)|(\/)+/g, "$1$2") | ||
.replace(/^(.+?)\/*?$/, "$1"); | ||
req.params = matchedFinal; | ||
} | ||
}); | ||
let matchedURL = patchRouters.find(router => { | ||
return router.value == matchedRe; | ||
}); | ||
if (matchedURL) { | ||
if ( | ||
matchedURL.method.toLowerCase() == "delete" | ||
) { | ||
res.statusCode = 200; | ||
let headers = req.headers; | ||
let comingData = ""; | ||
req.on("data", function(data) { | ||
comingData += data; | ||
}); | ||
req.on("end", function() { | ||
if (comingData.length > 0) { | ||
if ( | ||
headers["content-type"] === | ||
"application/x-www-form-urlencoded" | ||
) { | ||
comingData = querystring.parse(comingData); | ||
} else if (headers["content-type"] === "application/json") { | ||
comingData = JSON.parse(comingData); | ||
} else if ( | ||
headers["content-type"] === "text/plain" || | ||
headers["content-type"] === "text/plain;charset=UTF-8" | ||
) { | ||
try { | ||
comingData = JSON.parse(comingData); | ||
} catch (e) { | ||
comingData = comingData; | ||
} | ||
} | ||
req.data = comingData; | ||
} | ||
let middleWares = matchedURL.middleWares; | ||
if (middleWares && middleWares.length > 0) { | ||
var nexted = 0; | ||
middleWares[0](req, res, next); | ||
function next() { | ||
nexted++; | ||
if (middleWares.length <= nexted) { | ||
return matchedURL.handler(req, res); | ||
} else { | ||
return middleWares[nexted](req, res, next); | ||
} | ||
} | ||
} else { | ||
return matchedURL.handler(req, res); | ||
} | ||
}); | ||
} else { | ||
res.writeHead(404); | ||
return this.errorPage ? | ||
this.errorPage(req, res) : | ||
self.errorPage(req, res); | ||
} | ||
} else { | ||
res.writeHead(404); | ||
return this.errorPage ? | ||
this.errorPage(req, res) : | ||
self.errorPage(req, res); | ||
} | ||
} | ||
@@ -299,0 +621,0 @@ } |
{ | ||
"name": "grandjs", | ||
"version": "1.2.17", | ||
"version": "1.2.18", | ||
"description": "A backend framework for solid web apps based on node.js", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
102564
1348