Compare commits
4 Commits
9a36c1bf77
...
802f95f503
Author | SHA1 | Date | |
---|---|---|---|
|
802f95f503 | ||
|
978d433f8a | ||
|
73a91b4f48 | ||
|
f759902272 |
1
index.js
1
index.js
@ -11,5 +11,4 @@ gemini.router.use('/', index);
|
|||||||
gemini.router.use('/articles/', articles);
|
gemini.router.use('/articles/', articles);
|
||||||
gemini.router.use('/articles/:item', articles_item);
|
gemini.router.use('/articles/:item', articles_item);
|
||||||
|
|
||||||
|
|
||||||
gemini.listen();
|
gemini.listen();
|
||||||
|
@ -24,7 +24,7 @@ ${contents}
|
|||||||
}
|
}
|
||||||
|
|
||||||
function articles_item(request, response) {
|
function articles_item(request, response) {
|
||||||
var article = fs.readFileSync('./articles/' + decodeURIComponent(request.params.item));
|
var article = fs.readFileSync('./articles/' + request.params.item);
|
||||||
response.send(`20 text/gemini\r\n
|
response.send(`20 text/gemini\r\n
|
||||||
${article}
|
${article}
|
||||||
|
|
||||||
|
@ -42,7 +42,7 @@ module.exports = class Request {
|
|||||||
this.protocol = geminiUrl.protocol;
|
this.protocol = geminiUrl.protocol;
|
||||||
this.hostname = geminiUrl.hostname;
|
this.hostname = geminiUrl.hostname;
|
||||||
if (geminiUrl.port) this.port = geminiUrl.port;
|
if (geminiUrl.port) this.port = geminiUrl.port;
|
||||||
this.path = geminiUrl.pathname;
|
this.path = decodeURIComponent(geminiUrl.pathname);
|
||||||
this.search = geminiUrl.search.substring(1); //remove '?' from string
|
this.search = geminiUrl.search.substring(1); //remove '?' from string
|
||||||
if(this.protocol === 'titan:') {
|
if(this.protocol === 'titan:') {
|
||||||
this.content = this.rawRequest.slice(this.rawRequest.indexOf('\r\n') + 3);
|
this.content = this.rawRequest.slice(this.rawRequest.indexOf('\r\n') + 3);
|
||||||
|
@ -3,26 +3,20 @@
|
|||||||
|
|
||||||
function selectRoute(path, routes) {
|
function selectRoute(path, routes) {
|
||||||
result = -1;
|
result = -1;
|
||||||
// console.log(path);
|
|
||||||
var mPathComponents = path.split('/');
|
var mPathComponents = path.split('/');
|
||||||
routes.forEach((item, index) => {
|
routes.forEach((item, index) => {
|
||||||
// console.log(item);
|
|
||||||
var mitem = item.split('/');
|
var mitem = item.split('/');
|
||||||
if((mitem.length == mPathComponents.length)) {
|
if((mitem.length == mPathComponents.length)) {
|
||||||
if((item.indexOf(':') > 0) && (path.endsWith('/') == false)) {
|
if((item.indexOf(':') > 0) && (path.endsWith('/') == false)) {
|
||||||
// console.log('we need parameters');
|
|
||||||
var mmitem = item.substring(item.indexOf(':'));
|
var mmitem = item.substring(item.indexOf(':'));
|
||||||
var mmpath = path.substring(item.indexOf(':'));
|
var mmpath = path.substring(item.indexOf(':'));
|
||||||
var mmitemList = mmitem.split('/');
|
var mmitemList = mmitem.split('/');
|
||||||
var mmpathList = mmpath.split('/');
|
var mmpathList = mmpath.split('/');
|
||||||
if(mmitemList.length == mmpathList.length) {
|
if(mmitemList.length == mmpathList.length) {
|
||||||
// console.log('winner is here');
|
|
||||||
if(result == -1) result = index;
|
if(result == -1) result = index;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// console.log('we do not need parameters');
|
|
||||||
if(item == path) {
|
if(item == path) {
|
||||||
// console.log('winnere here');
|
|
||||||
if(result == -1) result = index;
|
if(result == -1) result = index;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -48,13 +42,6 @@ module.exports = class Router {
|
|||||||
this.request = request;
|
this.request = request;
|
||||||
this.response = response;
|
this.response = response;
|
||||||
|
|
||||||
/////////////////////////////////////
|
|
||||||
// console.log(selectRoute(path, this.routes));
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/////////////////////////////////////
|
|
||||||
|
|
||||||
var pathComponents = path.split('/');
|
var pathComponents = path.split('/');
|
||||||
for (var i = 0; i < pathComponents.length; i++) {
|
for (var i = 0; i < pathComponents.length; i++) {
|
||||||
pathComponents[i] = '/' + pathComponents[i];
|
pathComponents[i] = '/' + pathComponents[i];
|
||||||
|
@ -1,102 +0,0 @@
|
|||||||
// Uplink Gemini Server
|
|
||||||
// Router Class
|
|
||||||
|
|
||||||
function selectRoute(path, routes) {
|
|
||||||
result = -1;
|
|
||||||
// console.log(path);
|
|
||||||
var mPathComponents = path.split('/');
|
|
||||||
routes.forEach((item, index) => {
|
|
||||||
// console.log(item);
|
|
||||||
var mitem = item.split('/');
|
|
||||||
if((mitem.length == mPathComponents.length)) {
|
|
||||||
if((item.indexOf(':') > 0) && (path.endsWith('/') == false)) {
|
|
||||||
// console.log('we need parameters');
|
|
||||||
var mmitem = item.substring(item.indexOf(':'));
|
|
||||||
var mmpath = path.substring(item.indexOf(':'));
|
|
||||||
var mmitemList = mmitem.split('/');
|
|
||||||
var mmpathList = mmpath.split('/');
|
|
||||||
if(mmitemList.length == mmpathList.length) {
|
|
||||||
// console.log('winner is here');
|
|
||||||
if(result == -1) result = index;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// console.log('we do not need parameters');
|
|
||||||
if(item == path) {
|
|
||||||
// console.log('winnere here');
|
|
||||||
if(result == -1) result = index;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = class Router {
|
|
||||||
constructor() {
|
|
||||||
this.routes = [];
|
|
||||||
this.handlers = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
use(route, handler) {
|
|
||||||
this.routes[this.routes.length] = route;
|
|
||||||
this.handlers[this.handlers.length] = handler;
|
|
||||||
this.request = undefined;
|
|
||||||
this.response = undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
set(path, request, response) {
|
|
||||||
this.request = request;
|
|
||||||
this.response = response;
|
|
||||||
|
|
||||||
/////////////////////////////////////
|
|
||||||
// console.log(selectRoute(path, this.routes));
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/////////////////////////////////////
|
|
||||||
|
|
||||||
var pathComponents = path.split('/');
|
|
||||||
for (var i = 0; i < pathComponents.length; i++) {
|
|
||||||
pathComponents[i] = '/' + pathComponents[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
var routes = this.routes;
|
|
||||||
|
|
||||||
var paths = [];
|
|
||||||
var params = [];
|
|
||||||
|
|
||||||
var mindex = selectRoute(path, this.routes);
|
|
||||||
|
|
||||||
var routeComponents = routes[mindex].split('/');
|
|
||||||
paths = [];
|
|
||||||
params = [];
|
|
||||||
|
|
||||||
if (routeComponents.length == pathComponents.length) {
|
|
||||||
for (var k = 0; k < routeComponents.length; k++) {
|
|
||||||
routeComponents[k] = '/' + routeComponents[k];
|
|
||||||
if (routeComponents[k].startsWith('/:')) {
|
|
||||||
params.push(routeComponents[k]);
|
|
||||||
} else {
|
|
||||||
paths.push(routeComponents[k]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var finalParams = pathComponents.slice(paths.length);
|
|
||||||
for (var l = 0; l < finalParams.length; l++) {
|
|
||||||
finalParams[l] = finalParams[l].substring(1);
|
|
||||||
}
|
|
||||||
var myParams = {};
|
|
||||||
for (var m = 0; m < params.length; m++) {
|
|
||||||
eval('myParams.' + params[m].substring(2) + ' = "' + JSON.stringify(finalParams[m]).slice(1, -1) + '"');
|
|
||||||
}
|
|
||||||
|
|
||||||
this.request.params = myParams;
|
|
||||||
var mm = typeof this.handlers[mindex];
|
|
||||||
if (mm !== 'undefined') {
|
|
||||||
this.handlers[mindex](this.request, this.response);
|
|
||||||
} else {
|
|
||||||
this.response.error(51);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
@ -1,102 +0,0 @@
|
|||||||
// Uplink Gemini Server
|
|
||||||
// Router Class
|
|
||||||
|
|
||||||
function selectRoute(path, routes) {
|
|
||||||
result = -1;
|
|
||||||
console.log(path);
|
|
||||||
var mPathComponents = path.split('/');
|
|
||||||
routes.forEach((item, index) => {
|
|
||||||
console.log(item);
|
|
||||||
var mitem = item.split('/');
|
|
||||||
if((mitem.length == mPathComponents.length)) {
|
|
||||||
if((item.indexOf(':') > 0) && (path.endsWith('/') == false)) {
|
|
||||||
console.log('we need parameters');
|
|
||||||
var mmitem = item.substring(item.indexOf(':'));
|
|
||||||
var mmpath = path.substring(item.indexOf(':'));
|
|
||||||
var mmitemList = mmitem.split('/');
|
|
||||||
var mmpathList = mmpath.split('/');
|
|
||||||
if(mmitemList.length == mmpathList.length) {
|
|
||||||
console.log('winner is here');
|
|
||||||
if(result == -1) result = index;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
console.log('we do not need parameters');
|
|
||||||
if(item == path) {
|
|
||||||
console.log('winnere here');
|
|
||||||
if(result == -1) result = index;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = class Router {
|
|
||||||
constructor() {
|
|
||||||
this.routes = [];
|
|
||||||
this.handlers = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
use(route, handler) {
|
|
||||||
this.routes[this.routes.length] = route;
|
|
||||||
this.handlers[this.handlers.length] = handler;
|
|
||||||
this.request = undefined;
|
|
||||||
this.response = undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
set(path, request, response) {
|
|
||||||
this.request = request;
|
|
||||||
this.response = response;
|
|
||||||
|
|
||||||
/////////////////////////////////////
|
|
||||||
console.log(selectRoute(path, this.routes));
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/////////////////////////////////////
|
|
||||||
|
|
||||||
var pathComponents = path.split('/');
|
|
||||||
for (var i = 0; i < pathComponents.length; i++) {
|
|
||||||
pathComponents[i] = '/' + pathComponents[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
var routes = this.routes;
|
|
||||||
|
|
||||||
var paths = [];
|
|
||||||
var params = [];
|
|
||||||
|
|
||||||
var mindex = selectRoute(path, this.routes);
|
|
||||||
|
|
||||||
var routeComponents = routes[mindex].split('/');
|
|
||||||
paths = [];
|
|
||||||
params = [];
|
|
||||||
|
|
||||||
if (routeComponents.length == pathComponents.length) {
|
|
||||||
for (var k = 0; k < routeComponents.length; k++) {
|
|
||||||
routeComponents[k] = '/' + routeComponents[k];
|
|
||||||
if (routeComponents[k].startsWith('/:')) {
|
|
||||||
params.push(routeComponents[k]);
|
|
||||||
} else {
|
|
||||||
paths.push(routeComponents[k]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var finalParams = pathComponents.slice(paths.length);
|
|
||||||
for (var l = 0; l < finalParams.length; l++) {
|
|
||||||
finalParams[l] = finalParams[l].substring(1);
|
|
||||||
}
|
|
||||||
var myParams = {};
|
|
||||||
for (var m = 0; m < params.length; m++) {
|
|
||||||
eval('myParams.' + params[m].substring(2) + ' = "' + JSON.stringify(finalParams[m]).slice(1, -1) + '"');
|
|
||||||
}
|
|
||||||
|
|
||||||
this.request.params = myParams;
|
|
||||||
var mm = typeof this.handlers[mindex];
|
|
||||||
if (mm !== 'undefined') {
|
|
||||||
this.handlers[mindex](this.request, this.response);
|
|
||||||
} else {
|
|
||||||
this.response.error(51);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
Reference in New Issue
Block a user