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