Skip to content
Maladrumthrasky Louh edited this page Mar 25, 2015 · 14 revisions

#API References All these APIs can be used in activities.

Malache

This is a class which can help you to create activities, you can use it's instance in activities directly. The variable name of its instance is "malache".

require(String module)

this is referred to require() of node.

loadPlug(String module)

use require() to load the "module" in Plugs folder.

Flow

This is a class for optimizing the structure of activities code. With this, we can kill the deep callbacks. First, you should create an instance of Flow:

var flow=new malache.Flow;

add(Function step)

you should use this method to add a step like below:

flow.add(function()
{
    //...
});

this supports chain calls, so you can use it like this:

flow.add(function()
{
    //...
}).add(function()
{
    //...
});

start()

After you added all your steps into flow, you should call this method to start it up like below:

flow.add(function()
{
    //...
}).add(function()
{
    //...
}).start();

next()

This method will call the next step of the flow, you should use it like this:

var a=new Object;
flow.add(function()
{
    a.ok=1;
    this.next();
}).add(function()
{
    var that=this;
    setTimeout(function()
    {
        a.ko=100;
        that.next();
    },1000);
}).add(function()
{
    console.log(a);
}).start();

finish()

In your last step, you should call this method to clear the flow, it doesn't receive any argument.

##Response This class is used for respond content to client. you can use its instance named "response" in activities.

###render(Object data, [String template]) This method will render data with template and send the render result to client. The second parameter is optional, if not set, this method will use the template which has the same prefix filename of this activity to render.

response.render({bob: 20,alex: 21},"age_report.xml");

And then, in /Templates/age_report.xml, you can use the data:

<html>
<body>
<h1>Age Report</h1>
Bob's age: <%=bob%><br />
Alex's age: <%=alex%>
</body>
</html>

!See EJS's documents for grammar details

###statusCode This is a data member to specify the response status status code. To see HTTP document to know the meaning of the value.

response.statusCode=200;

###getClient() This method returns socket of client directly, it is not recommended.

var sock=response.getClient();
sock.write("TEST");

###headers This is an object that allows you to set http response headers.

response.headers["Content-Type"]="text/html;charset=utf-8";

###redirect(String url) This method responds a 302 content to the client, that the client will redirect into the url you set as the parameter you putted into this method.

response.redirect("/abc.html");

###error(Object error) This method responds a 500 content to the client, message column and stack message in the object you putted into this method will be sent to client.

response.error({message: "YOU GOT ERROR", stack: "at main.js: 30"});

###setCookie(String cookieName,String cookieValue) Add a cookie to the client.

response.setCookie("mdate",(new Date).getTime());

###sendHeaders() Send the headers first to the client. Be attention, after you using this method, you can use end() method in the activity, you should use sendBuffer() method to respond data manually.

response.sendHeaders();

###sendBuffer([Buffer | String] buff) Send a buffer or string to the client. Be attention, before you use this method, you should call sendHeaders() method manually first.

var fs=require("fs");
fs.readFile(home+"/images/ok.jpg","binary",function(chunk)
{
    response.sendHeaders();
    response.sendBuffer(chunk);
});

###write([Buffer | String] buff) Use this method to append a peace of data into response queue, it will not send the data to client immediately. In fact, all the data you appended into response queue will be sent after you call end() method.

for (var i=0;i<100;i++)
{
    response.write("!");
}

###end() This method will send headers and the content of response queue to client, and after using this method, response will become invalid.

for (var i=0;i<100;i++)
{
    response.write("!");
}
response.end();

##Request The instance of this class contains the data requested from client. you can use its instance named "request" in activities.

###GET This is an object contains the parameters from GET method.

###parameters This is an object contains all the parameters from the client whatever it is GET or POST.

###POST This is an object contains the parameters from POST method.

###method This is a string, it's the request method of this request.

console.log(request.method);
//GET, POST or others.

###url This is a string, it's the request url.

###version This is a string, it's the request http version.

###remoteAddress This is remote ip addres of this request.

###remotePort This is remote request port, it's a number.

###headers This is an object, it refers to request headers.

console.log(request.headers["Content-Length"]);

###getPostBuffer() This method returns the content of this request, it's a buffer.

console.log(request.getPostBuffer().toString());

###cookies This is an object that contains the client cookies, you can read them like this:

console.log(request.cookies["mdate"]);

##Session This is an object to save the session data.

session["OK"]=true;
console.log(session["OK"]);

##Sharer This class is for sharing data between all sessions, all domains. Use it carefully. You can use "sharer" as its variable name in activities directly.

###setKey(String keyName, Any keyValue, Function callback) Set a value with the name set as keyName in sharer, after finishing, callback will be invoked.

sharer.setKey("malpower","kakoyiiiiii",function()
{
    //do something.
});

###getKey(String keyName, Function callback) Get a value from sharer, use callback to receive the data.

sharer.getKey("malpower",function(data)
{
    console.log(data);
});

##home This is an environment variable in activities. It refers to /site/YOUR CURRENT SITE/Templates

var fs=malache.require("fs");
var pic=fs.readFileSync(home+"/images/ok.jpg","binary");
response.headers["Content-Length"]=pic.length;
response.headers["Content-Type"]="image/jpeg";
response.sendHeaders();
response.sendBuffer(pic);

Clone this wiki locally