Node.js 返回 JSON 數據


Node.js 返回 JSON 數據

request.end([data[, encoding]][, callback])

var http = require('http');

const log = console.log;

http.createServer(function (request, response) {
    // log(`pagehide event`, request)
    // log(`pagehide event`, request.headers)
    // log(`pagehide event`, request.rawHeaders)
    // log(`page visibility`, request)
    log(`page visibility`)
    // 發送 HTTP 頭部 
    // HTTP 狀態值: 200 : OK
    // 內容類型: text/plain
    // response.writeHead(200, {'Content-Type': 'text/plain'});
    response.writeHead(200, {'Content-Type': 'application/json'});

    // 發送響應數據 "Hello World"
    // response.end('Hello World\n');
    response.end(JSON.stringify({
        name: "server",
        age: 2020,
    }));
    // .json
}).listen(8888);

// 終端打印如下信息
console.log('Server running at http://127.0.0.1:8888/');

https://nodejs.org/api/http.html#http_request_end_data_encoding_callback

https://nodejs.org/api/http.html#http_request_write_chunk_encoding_callback

refs



©xgqfrms 2012-2020

www.cnblogs.com 發布文章使用:只允許注冊用戶才可以訪問!



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM