如何将AWS nodejs输出的输出发送到浏览器而不是终端



我正在编写一个nodejs应用程序,它应该将输出输出到屏幕上,而不是console.log。

我的代码:

var AWS = require('aws-sdk');
AWS.config.update({accessKeyId: 'AK*************37D', secretAccessKey: 'DyP*****************************7THvRfro', region: 'ap-south-1'});
var http=require('http')
var server=http.createServer((function(request,response)
{
response.writeHead(200,
{"Content-Type" : "text/plain"});
response.end("Hello Worldn");
// Create S3 service object
s3 = new AWS.S3({apiVersion: '2006-03-01'});
//Call S3 to list the buckets
s3.listBuckets(function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data.Buckets);
}
});
var params = { 
Bucket: 'lfs-test-mb',
Delimiter: '',
//Prefix: 's/5469b2f5b4292d22522e84e0/ms.files' 
}
s3.listObjects(params, function (err, data) {
if(err)throw err;
console.log(data);
});
}));
server.listen(7000);

这段代码运行得很好,我可以运行"http://localhost:7000"将输出返回到终端。

D:AWS_NodeJS>node lfs.js
{
IsTruncated: false,
Marker: '',
Contents: [
{
Key: '39e8e2ab94c5ca5388cecf49481a05738e1d6ceaf127a48202f2ea670f9755ec',
LastModified: 2020-04-22T00:01:03.000Z,
ETag: '"40a62c83b10fe56bc9894d60631992a5"',
Size: 31457280,
StorageClass: 'REDUCED_REDUNDANCY',
Owner: [Object]
},
{
Key: '4a474cd829d49e686e1a998b4700955e024a17e62d35eb576973429f9159f2e6',
LastModified: 2020-04-24T04:56:32.000Z,
ETag: '"eb0542a9e23c8bfcf756e747502722f4"',
Size: 11690801,
StorageClass: 'REDUCED_REDUNDANCY',
Owner: [Object]
},
],
Name: 'lfs-test-mb',
Prefix: '',
MaxKeys: 1000,
CommonPrefixes: []
}

如何将响应发送到屏幕而不是终端????

console.log会将消息打印到终端。如果你想在浏览器中显示消息,你应该在response.end之前通过response.send(…(返回消息

response.writeHead(200, {"Content-Type" : "text/plain"});
response.send(data.Buckets);
response.end();

相关内容

  • 没有找到相关文章

最新更新