查看: 95|回覆: 0

Node.js 返回 JSON 数据

[複製鏈接]

2

主題

0

回帖

0

積分

热心网友

金币
0
閲讀權限
220
精華
0
威望
0
贡献
0
在線時間
0 小時
註冊時間
2008-4-3
發表於 2020-12-21 17:07:00 | 顯示全部樓層 |閲讀模式

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 发布文章使用:只允许注册用户才可以访问!


回覆

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即注册

本版積分規則

相关侵权、举报、投诉及建议等,请发 E-mail:qiongdian@foxmail.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部