所以我正在构建云功能,并且将代码结构化为多个文件。
我的index.js如下:
const { postHandler } = require('./requestHandler.js');
const httpHandler = (req, res) => {
const { method } = req;
switch (method) {
case 'POST':
postHandler(req, res);
break;
case 'GET':
getHandler(req, res);
break;
case 'DELETE':
deleteHandler(req, res);
break;
case 'PUT':
putHandler(req, res);
break;
default:
return res.sendStatus(400).json({message: 'Invalid method. Allowed are POST, GET, DELETE'});
}
}
exports.httpHandler = httpHandler
这是requestHandler
const createReviewApp = require('./createReviewApp')
const postHandler = ( req, res ) => {
const { path } = req;
return createReviewApp( req, res )
}
module.exports = { postHandler }
这是createReviewApp
const _compute = require('./computeClass.js');
const _dns = require('./dnsClass.js');
const _storage = require('./storageClass.js');
const createReviewApp = ( req, res ) => {
// doing something
}
这是我的computeClass
const Compute = require('@google-cloud/compute')
const projectId = process.env.GOOGLE_PROJECT_ID;
class ComputeSingleton {
constructor(){
this.compute = new Compute({
projectId,
});
}
getVm({zoneName, vmName}){
return this.getZone({zoneName}).vm(vmName).get();
}
getZone({zoneName}){
return this.compute.zone(zoneName);
}
createVm({zoneName, vmName, config}){
return this.getZone({zoneName}).createVM(vmName, config);
}
deleteVm({zoneName, vmName}){
return this.getZone({zoneName}).vm(vmName).delete();
}
}
module.exports = new ComputeSingleton()
现在,在我的computeClass中,出现以下错误:
Cannot find module '@google-cloud/compute
我已经安装了依赖项,我是否需要将所有代码放在一个文件中?
编辑:我正在使用nodejs模拟器功能对其进行测试
答案 0 :(得分:0)
否,您不需要将代码存储在一个文件中。听起来您实际上在功能项目package.json中没有@google-cloud/compute
并没有完全安装。