Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.

Latest commit

 

History

History
128 lines (86 loc) · 2.82 KB

HttpAnswer.md

File metadata and controls

128 lines (86 loc) · 2.82 KB

HttpAnswer

HttpAnswer is the wrapper on original HTTP response instance. This is passed as 2nd parameter to any request handler.

Properties

encoding : Default is "utf-8"

Methods

type : set Content-Type

answer.type("application/json");
var contentType = answer.type();

length : set Content-Length

answer.length(35);//in bytes
var contentLength = answer.length();

getHeader : get header parameter value. Header name is case insensitive.

answer.getHeader("header-name");

cookie : Set cookie header value. The value should be serialized with all attributes, like path, domain, expiry time etc.

answer.cookie("sessionToken=abc123; Expires=Wed, 09 Jun 2021 10:18:14 GMT");

You can use some external library for serialization. Eg

var cookie = require('cookie');

muneem.add("handler", (asked, answer) => {
    //..
    answer.cookie( cookie.serialize(name, val, attributes) );
});

setHeader : set header parameter value. Header name is case insensitive and overwrite privious value.

answer.setHeader("header-name", value);

removeHeader : remove header parameter. Header name is case insensitive.

answer.removeHeader("header-name");

answered : return true if the request is already answered.

answer.answered();

status : Set status code.

answer.status(200);

write : write data to response.

//answer.write(data [,type [,length [, safe ] ] ]);
answer.write("I'm fine.", "plain/text", 9);
answer.end();

end : End the response stream and finally send the data to client.

Once called all the remaining handlers and their pre/post handler event callbacks will be canceled.

//answer.end(type ,length [, reason]);
//answer.end(code [, reason]);
//answer.end([ reason]);
answer.end();

Specifying the reason may helpful when client is answered abnormally due to the error, invalid input, server issue etc. It helps in error reporting, logging.

close : Lighter version of end(). Useful for non-200 responses. It doesn't invoke before/after answer event, it doesn't response with data.

Once called all the remaining handlers and their pre/post handler event callbacks will be canceled.

//answer.close(code [, reason]);
answer.close(404);

Specifying the reason may helpful in error reporting, logging.

skip : Once called number of next handlers and their pre/post handler event callbacks will be skipped.

//answer.close(code [, reason]);
answer.skip(2);

redirectTo : Redirect the current request to given location with 302 status code.

answer.redirectTo(location);

error : This method invokes error handler.

answer.error(new Error());

resourceNotFound : This method invokes route not found handler

answer.resourceNotFound(new Error());