<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                [TOC=2,4] 這里的 http 對象并不是 Node.js 里的 http 模塊,而是對 request 和 response 對象包裝后一個新的對象。 ~~~ var http = require("http"); http.createServer(function (request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); response.end("Hello World\n"); }).listen(8124); ~~~ 如上面的代碼所示,Node.js 創建服務時,會傳遞 request 和 response 2個對象給回調函數。為了后續調用方便, ThinkJS 對這2個對象進行了包裝,包裝成了 http 對象,并且提供很多有用的方法。 http 對象會在 middleware, logic, controller, view 中傳遞。 `注`:http 對象是 EventEmitter 的一個實例,所以可以對其進行事件監聽和執行。 ### 屬性 #### http.req 系統原生的 request 對象 #### http.res 系統原生的 response 對象 #### http.startTime 請求的開始時間,是個`unix`時間戳。 #### http.url 當前請求的 url 。 #### http.version 當前請求的 http 版本。 #### http.method 當前請求的類型。 #### http.headers 當前請求的所有頭信息。 #### http.pathname 當前請求的 pathname,路由識別會依賴該值,會在后續的處理中對其進行改變。所以在 action 拿到值可能跟初始解析出來的值不一致。 #### http.query 當前請求的所有 query 數據。 #### http.host 當前請求的 host, 包含端口。 #### http.hostname 當前請求的 hostname,不包含端口。 #### http.payload 當前請求的 payload 數據,提交型的請求才含有該值。 #### http._payloadParsed 表示當前請求的 payload 數據是否已經解析。 #### http._get 存放 GET 參數值。 #### http._post 存放 POST 參數值 #### http._file 存放上傳的文件數據 #### http._cookie 存放 cookie 數據。 ### 方法 #### http.config(name) * `name`?{String} 參數名 * `return`?{Mixed} 返回對應的參數值 獲取當前請求下對應的參數值。 #### http.referrer() * `return`?{String} 請求的 referrer 返回當前請求的 referrer。 #### http.userAgent() * `return`?{String} 請求的 userAgent 返回當前請求的 userAgent。 #### http.isGet() * `return`?{Boolean} 返回當前請求是否是 GET 請求。 #### http.isPost() * `return`?{Boolean} 返回當前請求是否是 POST 請求。 #### http.isAjax(method) * `method`?{String} 請求類型 * `return`?{Boolean} 返回當前請求是否是 Ajax 請求。 ~~~ http.isAjax(); //判斷是否是Ajax請求 http.isAjax("GET"); //判斷是否是Ajax請求,且請求類型是GET ~~~ #### http.isJsonp(name) * `name`?{String} callback 參數名稱,默認為 callback * `return`?{Boolean} 返回當前請求是否是 jsonp 請求。 ~~~ //url is /index/test?callback=testxxx http.isJsonp(); //true http.isJsonp("cb"); //false ~~~ #### http.get(name, value) * `name`?{String} 參數名稱 * `value`?{Mixed} 參數值 獲取或者設置 GET 參數值。可以通過該方法設置 GET 參數值,方便后續的邏輯里獲取。 ~~~ // url is /index/test?name=thinkjs http.get("name"); // returns "thinkjs" http.get("name", "other value"); http.get("name"); // returns "other value" ~~~ #### http.post(name, value) * `name`?{String} 參數名稱 * `value`?{Mixed} 參數值 獲取或者設置 POST 值。可以通過該方法設置 POST 值,方便后續的邏輯里獲取。 ~~~ http.post("email"); //獲取提交的email ~~~ #### http.param(name) * `name`?{String} 參數名稱 * `return`?{Mixed} 獲取參數值,優先從 POST 里獲取,如果值為空,則從 URL 參數里獲取。 #### http.file(name) * `name`?{String} 文件對應的字段名稱 * `return`?{Object} 獲取上傳的文件。 ~~~ http.file("image"); //returns { fieldName: "image", //表單里的字段名 originalFilename: filename, //原始文件名 path: filepath, //文件臨時存放的路徑 size: size //文件大小 } ~~~ #### http.header(name, value) * `name`?{String} header 名稱 * `value`?{String} header 值 獲取或者設置 header 信息。 ~~~ http.header("accept"); //獲取accept http.header("X-NAME", "thinkjs"); //設置header ~~~ #### http.expires(time) * `time`?{Number} 過期時間,單位為秒 強緩存,設置?`Cache-Control`?和?`Expires`?頭信息。 ~~~ http.header(86400); //設置過期時間為 1 天。 ~~~ #### http.status(status) 設置狀態碼。如果頭信息已經發送,則無法設置狀態碼。 ~~~ http.status(400); //設置狀態碼為400 ~~~ #### http.ip() 獲取用戶的 ip 。如果使用了代理,獲取的值可能不準。 #### http.lang(lang, asViewPath) * `lang`?{String} 要設置的語言 * `asViewPath`?{Boolean} 是否添加一層模版語言目錄 獲取或者設置國際化的語言,可以支持模版路徑要多一層語言的目錄。 ##### 獲取語言 ~~~ let lang = http.lang(); ~~~ 獲取語言的循序為?`http._lang`?->?`從 cookie 中獲取`?->?`從 header 中獲取`,如果需要從 url 中解析語言,可以獲取后通過?`http.lang(lang)`?方法設置到屬性?`http._lang`?中。 ##### 設置語言 ~~~ let lang = getFromUrl(); http.lang(lang, true); //設置語言,并指定模版路徑中添加一層語言目錄 ~~~ #### http.theme(theme) 獲取或者設置主題,設置后模版路徑要多一層主題的目錄。 #### http.cookie(name, value) * `name`?{String} cookie 名稱 * `value`?{String} cookie 值 讀取或者設置 cookie 值。 ~~~ http.cookie("think_test"); //獲取名為 think_test 的 cookie http.cookie("name", "value"); //設置 cookie,如果頭信息已經發送則設置無效 ~~~ #### http.session(name, value) * `name`?{String} session 名 * `value`?{Mixed} session 值 * `return`?{Promise} 讀取、設置和清除 session。 ##### 讀取 Session ~~~ let value = yield http.session("userInfo"); ~~~ ##### 設置 Session ~~~ yield http.session("userInfo", data); ~~~ ##### 清除 Session ~~~ yield http.session(); ~~~ #### http.redirect(url, status) * `url`?{String} 要跳轉的 url * `status`?{Number} 狀態碼, 301 或者 302,默認為302 頁面跳轉。 ~~~ http.redirect("/login"); //跳轉到登錄頁面 ~~~ #### http.type(contentType, encoding) * `contentType`?{String} 要設置的 contentType * `encoding`?{String} 要設置的編碼 獲取或者設置 Content-Type。 ~~~ http.type(); //獲取Content-Type http.type("text/html"); //設置Content-Type,會自動加上charset http.type("audio/mpeg", false); //設置Content-Type,不追加charset ~~~ #### http.write(content, encoding) * `content`?{Mixed} 要輸出的內容 * `encoding`?{String} 編碼 輸出內容,要調用 http.end 才能結束當前請求。 #### http.end(content, encoding) * `content`?{Mixed} 要輸出的內容 * `encoding`?{String} 編碼 輸出內容并結束當前請求。 #### http.success(data, message) * `data`?{Mixed} 要輸出的數據 * `message`?{String} 追加的message 格式化輸出一個正常的數據,一般是操作成功后輸出。 ~~~ http.success({name: "thinkjs"}); //writes { errno: 0, errmsg: "", data: { name: "thinkjs" } } ~~~ 這樣客戶端就可以根據`errno`是否為`0`為判斷當前請求是否正常。 #### http.fail(errno, errmsg, data) * `errno`?{Number} 錯誤號 * `errmsg`?{String} 錯誤信息 * `data`?{Mixed} 額外的數據 格式化輸出一個異常的數據,一般是操作失敗后輸出。 `注`:字段名`errno`和`errmsg`可以在配置里進行修改。 ~~~ http.fail(100, "fail") //writes { errno: 100, errmsg: "fail", data: "" } ~~~ 這樣客戶端就可以拿到具體的錯誤號和錯誤信息,然后根據需要顯示了。 `注`:字段名`errno`和`errmsg`可以在配置里進行修改。 #### http.json(data) * `data`?{Object} json 方式輸出數據,會設置 Content-Type 為?`application/json`,該值對應的配置為`json_content_type`。 文檔地址:[https://github.com/75team/www.thinkjs.org/tree/master/view/zh-CN/doc/2.0/api_http.md](https://github.com/75team/www.thinkjs.org/tree/master/view/zh-CN/doc/2.0/api_http.md)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看