查看: 137|回复: 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:dzh188@hotmail.com

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

在本版发帖返回顶部