This http object is not the one in Node.js, it is a new object which packaged with request object and response object.

var http = require('http');

http.createServer(function (request, response) {
  response.writeHead(200, {'Content-Type': 'text/plain'});
  response.end('Hello World\n');

As the above code shows, when Node.js create service, it will pass request and respone to callback. For the convenience of invoke, ThinkJS packaged these two objects into its own http object, and offer some useful functions.

The http object will be passed to middleware, logic, controller and view.

Note: http object is an instance of EventEmitter, so you register event listeners to it.



System native request object.


System native response object.


A start time of request, it is a unix timestamp.


Url of urrent request.


Http version of current request.


Type of current request.


Header informations of current request.


Pathname of current request, router depended on it's value and will change it in some operations. so the return value of action maybe different from the initial value.


Query data of current request.

Host of current request, contain host port.


Host of current request, not contain host port.


Payload data of current request, it has data only if the request is submit type.


Store GET arguments.


Store POST arguments.


Store upload file data.


Store cookie data.


The module name of current request parsed.


The controller name of current request parsed.


The action name of current request parsed.



  • return {Promise} payload data

Get payload data.


  • name {String} config name
  • return {Mixed} return config value

Get the argument of current request config.


  • return {String} referrer of request

Return the referrer of current request.


  • return {String} userAgent of request

Return the userAgent of current request.


  • return {Boolean}

Return current request is GET request or not.


  • return {Boolean}

Return current request is POST request or not.


  • method {String} type of request
  • return {Boolean}

Return current request is Ajax request or not.

http.isAjax(); // judge request is ajax request or not
http.isAjax('GET'); // judge request is ajax request and is GET type or not


  • name {String} callback parameter name, default is callback
  • return {Boolean}

Return current request is jsonp request or not.

//url is  /index/test?callback=testxxx
http.isJsonp(); //true
http.isJsonp('cb'); //false

http.get(name, value)

  • name {String} parameter name
  • value {Mixed} parameter value

Get or set GET parameter, it can be used to set GET argument for somewhere can get it.

// url is /index/test?name=thinkjs
http.get('name'); // returns 'thinkjs'
http.get('name', 'other value');
http.get('name'); // returns 'other value', value)

  • name {String} parameter name
  • value {Mixed} parameter value

Get or set POST parameter, it can be used to set POST argument for somewhere can get it.'email'); // get the submited email


  • name {String} parameter name
  • return {Mixed}

Get parameter value, firstly to get from POST, if return null, it will get the value from URL parameter.


  • name {String} field name
  • return {Object}

Get the uploaded file.

  fieldName: 'image', // the filed name in form
  originalFilename: filename, // origin file name
  path: filepath, // the temp path of store files
  size: size // file size

http.header(name, value)

  • name {String} header name
  • value {String} header value

Get or set header information.

http.header('accept'); // get accept
http.header('X-NAME', 'thinkjs'); // set header


  • time {Number} expire time, unit is second.

Strange cache, set Cache-Control and Expries header inforamtion.

http.header(86400); // set expire time is one day.


set status code, if header has sent, it cannot set status code.

http.status(400); // set status code to 400


Get user's ip, it will been incorrect if user used proxy.

http.lang(lang, asViewPath)

  • lang {String} the setup of language.
  • asViewPath {Boolean} whether add a directory layer for language template.

Get or set global language, it support more directory layer for language template.

Get language
let lang = http.lang();

The order to get language is http._lang -> get from cookie -> get from header, if need to parse language from url, you can set http._lang with http.lang(lang) after get url.

set language
let lang = getFromUrl();
http.lang(lang, true); // set language, and set a directory layer for language template.


Get or set theme, after setting, it will generate a lay for theme.

http.cookie(name, value)

  • name {String} cookie name
  • value {String} cookie value

Read or set cookie.

http.cookie('think_test'); // get cookie named think_test
http.cookie('name', 'value'); // get cookie, invalid if header has sent.

http.session(name, value)

  • name {String} session name
  • value {Mixed} session value
  • return {Promise}

Read, set and clean session.

Read Session
let value = await http.session('userInfo');
set Session
await http.session('userInfo', data);
clean Session
await http.session();

http.redirect(url, status)

  • url {String} the url will jump
  • status {Number} status code, 301 or 302, default is 302.

Jump page.

http.redirect('/login'); // jump to login page.

http.type(contentType, encoding)

  • contentType {String} contentType which need to modify
  • encoding {String} encode to set

Read or set Content-Type.

http.type(); // get Content-Type
http.type('text/html'); // get Content-Type, it will add charset automatically
http.type('audio/mpeg', false); // set Content-Type, not add charset

http.write(content, encoding)

  • content {Mixed} the content to write
  • encoding {String} charset

Write content, end request only invoke http.end.

http.end(content, encoding)

  • content {Mixed} the content to write
  • encoding {String} charset

Write content and stop current request.

http.success(data, message)

  • data {Mixed} the content to write
  • message {String} added message

Response a format normal data , always after operate success.

http.success({name: 'thinkjs'});
  errno: 0,
  errmsg: '',
  data: {
    name: 'thinkjs'

Client can based on error is 0 or not to judge current request is success., errmsg, data)

  • errno {Number} error number
  • errmsg {String} error message
  • data {Mixed} extra data

Output an unusual formatted data, normally after operate failed.

Notice: field name errno and errmsg can been modified in config., 'fail')
  errno: 100,
  errmsg: 'fail',
  data: ''

In this way, client will get detail error number and error message, then show message according to the need.

Notice: filed name errno and errmsg can been modified in config.


  • data {Object}

Output data in json way, it will set Content-Type to application/json, its config is json_content_type.