After installing by "npm install impress" you can take example application for testing it:
Handler examples and file system url mapping:
1. Template example
Location: http://localhost
Base template: /sites/localhost/html.template
2. Override included "left.template"
Location: http://localhost/override
Overriden template: /sites/localhost/override/left.template
Base template: /sites/localhost/html.template
Handler: /sites/localhost/request.js
3. JSON api method example
Location: http://localhost/api/examples/methodName.json
Handler: /sites/localhost/api/examples/methodName.json/get.js
4. Start anonymous session
Location: http://localhost/api/auth/anonymousSession.json
Handler: /sites/localhost/api/auth/anonymousSession.json/get.js
5. POST request handler
Location: POST http://localhost/api/auth/regvalidation.json
Handler: /sites/localhost/api/auth/regvalidation.json/post.js
6. MongoDB access example
Location: http://localhost/api/examples/getUsers.json
Handler: /sites/localhost/api/examples/getUsers.json/get.js
Filesystem routing is project a forming feature of Impress, here is
Following "server.js" is stating file. Run it using command line "node server" for debud or "nohup node server" for production.
require('impress');
impress.init(function() {
// Place here other initialization code
// to be executed after Impress initialization
});
File "access.js" is something line ".htaccess", you can easily define access restrictions for each folder, placing "access.js" in it.
If folder not contains "access.js" it will inherit from parent folder and so on. Example:
module.exports = {
guests: true, // allow requests from not logged users
logged: true, // allow requests from logged users
http: true, // allow requests using http protocol
https: true, // allow requests using https protocol (SSL)
groups: [] // allow access for user groups listed in array
// or for all if array is empty or no groups field specified
}
File "request.js": place such file in folder to be executed on each request (GET, POST, PUT, etc.).
If folder not contains "request.js" it will inherit from parent folder and so on. Example:
module.exports = function(req, res, callback) {
res.context.data = {
title: "Page Title",
users: [
{ name: "vasia", age: 22, emails: ["us...@gmail.com", "us...@gmail.com"] },
{ name: "dima", age: 32, emails: ["us...@gmail.com", "us...@gmail.com", "us...@gmail.com"] },
],
session: JSON.stringify(impress.sessions[req.impress.session])
};
callback();
}
File "get.js": place such file in folder to be executed on GET request. For POST request "post.js", and so on.
If folder not contains "get.js" it will inherit from parent folder and so on. Example:
module.exports = function(req, res, callback) {
db.polltool.query('select * from City', function(err, rows, fields) {
if (err) throw err;
res.context.data = { rows:rows, fields:fields };
callback();
});
}
File "html.template": place such file in folder as a main page template. Example:
<!DOCTYPE html>
<html>
<head>
<title>@title@</title>
</head>
<body>
<div> Field value: @field@ </div>
<div> Include template: @[name]@ - this will include file "./name.template" </div>
<div> This will iterate "res.context.data" from "request.js" example above:
@[users]@
<div>
User name: @.name@<br/>
User age: @.age@<br/>
Email addresses:
<ul>
@[emails]@
<li>@.value@</li>
@[/emails]@
</ul>
</div>
@[/users]@
</div>
</body>
</html>
Here are links:
Thank for your time and feedback,
Best regards,
~Timur Shemsedinov