我正在设置一个新服务器,该服务器支持API请求,这些请求会生成txt文件并将其保存到Firebase存储中。
这是针对herokuapp中的节点js服务器的,我正在使用express,body-parser和firebase admin。当我尝试运行代码时,服务器崩溃。
async function uploadFile(bucketName, filename) {
// [START storage_upload_file]
// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');
// Creates a client
const storage = new Storage();
var path = `test/${filename}`
// Uploads a local file to the bucket
await storage.bucket(bucketName).upload(path, {
// Support for HTTP requests made with `Accept-Encoding: gzip`
gzip: true,
metadata: {
cacheControl: 'no-cache', //the contents will change
},
});
console.log(`${filename} uploaded to ${bucketName}.`);
// [END storage_upload_file]
}
app.get('/test', async (req, res) => {
var path = req.body.path
var filename = req.body.filename
var content = req.body.content
var filepath = `${filename}.txt`
fs.writeFile(filepath, content, function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
uploadFile(MY-BUCKET, filepath)
});
});
最后,api应该生成简单的txt文件,并在每次出现新请求时编辑其内容。