Преглед изворни кода

modified the framework more

master
mort пре 8 година
родитељ
комит
198980ef0e

+ 28
- 4
index.js Прегледај датотеку

var conf = JSON.parse(fs.readFileSync("conf.json")); var conf = JSON.parse(fs.readFileSync("conf.json"));


var endpoints = { var endpoints = {
"/": "index.html",

//General files
"/favicon.ico": "favicon.ico",
"/bootstrap.css": "bootstrap.css",
"/404": "404.html", "/404": "404.html",
"/viewer": "viewer.node.js"

//Index files
"/": "index/index.node.js",
"/index/script.js": "index/script.js",
"/index/style.css": "index/style.css",

//Viewer files
"/viewer": "viewer/index.node.js",
"/viewer/script.js": "viewer/script.js",
"/viewer/style.css": "viewer/style.css"
} }


//Prepare endpoints //Prepare endpoints
//If it doesn't end with .node.js, it's a regular text file and will //If it doesn't end with .node.js, it's a regular text file and will
//just be served as is //just be served as is
} else { } else {
endpoints[i] = fs.readFileSync(conf.webroot+"/"+endpoints[i], "utf8");
endpoints[i] = fs.readFileSync(conf.webroot+"/"+endpoints[i]);
} }


//Errors will usually be because an endpoint doesn't exist //Errors will usually be because an endpoint doesn't exist
templates[f.replace(/\.html$/, "")] = fs.readFileSync("templates/"+f, "utf8"); templates[f.replace(/\.html$/, "")] = fs.readFileSync("templates/"+f, "utf8");
}); });


//Prepare all views
var views = {};
fs.readdirSync("views").forEach(function(f) {
views[f.replace(/\.html$/, "")] = fs.readFileSync("views/"+f, "utf8");
});

//Function to run on each request //Function to run on each request
function onRequest(req, res) { function onRequest(req, res) {
console.log("Request for "+req.url); console.log("Request for "+req.url);


//Execute if it's a .node.js, or just respond with the contents of the file //Execute if it's a .node.js, or just respond with the contents of the file
if (typeof ep == "function") { if (typeof ep == "function") {
ep(new Context(req, res, templates, conf));
ep(new Context({
req: req,
res: res,
templates: templates,
views: views,
conf: conf
}));
} else { } else {
res.end(ep); res.end(ep);
} }

+ 25
- 10
lib/context.js Прегледај датотеку

module.exports = function(req, res, templates, conf) {
this.req = req;
this.res = res;
this.templates = templates;
this.conf = conf;
function templatify(str, args) {
if (args == undefined)
return str;

for (var i in args) {
str = str.split("{{"+i+"}}").join(args[i]);
}

return str;
}

module.exports = function(options) {
this.req = options.req;
this.res = options.res;
this.templates = options.templates;
this.views = options.views;
this.conf = options.conf;
} }


module.exports.prototype = { module.exports.prototype = {


template: function(name, args) { template: function(name, args) {
var str = this.templates[name]; var str = this.templates[name];

if (!str) if (!str)
throw new Error("No such template: "+name); throw new Error("No such template: "+name);


for (var i in args) {
str = str.split("{{"+i+"}}").join(args[i]);
}
return templatify(str, args);
},


return str;
view: function(name, args) {
var str = this.views[name];
if (!str)
throw new Error("No such view: "+name);

return templatify(str, args);
} }
} }

+ 9
- 0
templates/header.html Прегледај датотеку

<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<div class="pull-right">
Log In
</div>
</div>
</div>
</div>

web/index.html → views/index.html Прегледај датотеку

<html> <html>
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<link rel="stylesheet" href="/bootstrap.css">
</head> </head>
<body> <body>
Hi.
{{header}}
</body> </body>
</html> </html>

templates/viewer.html → views/viewer.html Прегледај датотеку

<title>{{title}}</title> <title>{{title}}</title>
</head> </head>
<body> <body>
{{header}}
</body> </body>
</html> </html>

+ 9
- 0
web/bootstrap.css
Разлика између датотеке није приказан због своје велике величине
Прегледај датотеку



+ 5
- 0
web/index/index.node.js Прегледај датотеку

module.exports = function(ctx) {
ctx.end(ctx.view("index", {
header: ctx.template("header")
}));
}

+ 0
- 0
web/index/script.js Прегледај датотеку


+ 0
- 0
web/index/style.css Прегледај датотеку


+ 0
- 5
web/viewer.node.js Прегледај датотеку

module.exports = function(ctx) {
api.end(api.template("viewer", {
title: "lol hi"
}));
}

+ 5
- 0
web/viewer/index.node.js Прегледај датотеку

module.exports = function(ctx) {
ctx.end(ctx.view("viewer", {
header: ctx.template("header")
}));
}

+ 0
- 0
web/viewer/script.js Прегледај датотеку


+ 0
- 0
web/viewer/style.css Прегледај датотеку


Loading…
Откажи
Сачувај