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');
}).listen(8124);
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 dataGet payload data.
name
{String} config namereturn
{Mixed} return config valueGet the argument of current request config.
return
{String} referrer of requestReturn the referrer of current request.
return
{String} userAgent of requestReturn 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 requestreturn
{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 callbackreturn
{Boolean}Return current request is jsonp request or not.
//url is /index/test?callback=testxxx
http.isJsonp(); //true
http.isJsonp('cb'); //false
name
{String} parameter namevalue
{Mixed} parameter valueGet 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'
name
{String} parameter namevalue
{Mixed} parameter valueGet or set POST parameter, it can be used to set POST argument for somewhere can get it.
http.post('email'); // get the submited email
name
{String} parameter namereturn
{Mixed}Get parameter value, firstly to get from POST, if return null, it will get the value from URL parameter.
name
{String} field namereturn
{Object} Get the uploaded file.
http.file('image');
//returns
{
fieldName: 'image', // the filed name in form
originalFilename: filename, // origin file name
path: filepath, // the temp path of store files
size: size // file size
}
name
{String} header namevalue
{String} header valueGet 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.
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.
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.
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.
name
{String} cookie namevalue
{String} cookie valueRead or set cookie.
http.cookie('think_test'); // get cookie named think_test
http.cookie('name', 'value'); // get cookie, invalid if header has sent.
name
{String} session namevalue
{Mixed} session valuereturn
{Promise}Read, set and clean session.
let value = await http.session('userInfo');
await http.session('userInfo', data);
await http.session();
url
{String} the url will jumpstatus
{Number} status code, 301 or 302, default is 302.Jump page.
http.redirect('/login'); // jump to login page.
contentType
{String} contentType which need to modifyencoding
{String} encode to setRead 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
content
{Mixed} the content to writeencoding
{String} charsetWrite content, end request only invoke http.end.
content
{Mixed} the content to writeencoding
{String} charsetWrite content and stop current request.
data
{Mixed} the content to writemessage
{String} added messageResponse a format normal data , always after operate success.
http.success({name: 'thinkjs'});
//writes
{
errno: 0,
errmsg: '',
data: {
name: 'thinkjs'
}
}
Client can based on error
is 0
or not to judge current request is success.
errno
{Number} error numbererrmsg
{String} error messagedata
{Mixed} extra dataOutput an unusual formatted data, normally after operate failed.
Notice
: field name errno
and errmsg
can been modified in config.
http.fail(100, 'fail')
//writes
{
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
.