【问题标题】:App is not defined in google cloud function with express应用程序未在谷歌云功能中使用 express 定义
【发布时间】:2019-12-30 10:28:05
【问题描述】:

我正在尝试使用谷歌云功能进行一些 API 调用。当我在谷歌功能上部署时,网站向我发送错误:Function failed on loading user code. Error message: Code in file app.js can't be loaded. Is there a syntax error in your code? Detailed stack trace: ReferenceError: app is not defined

这可能是什么原因?

这些是文件:

//app.js

exports.simple = app;

App.js 应该调用执行函数调用“简单”的谷歌云函数 图片:1

// server.js

// BASE SETUP
var express = require('express');
var app     = express();
const port  = 8080;

// ROUTES
var router = express.Router(); // get an instance of router
router.use(function(req, res, next) { // route middleware that will happen on every request
 console.log(req.method, req.url); // log each request to the console
 next(); // continue doing what we were doing and go to the route
});

app.use('/',require ('./Routers/API/home'));

app.use('/leads',require ('./Routers/API/leads'));

app.use('/people',require ('./Routers/API/people'));

app.use('/companies',require ('./Routers/API/companies'));

app.use('/opportunities',require ('./Routers/API/opportunities'));

app.use('/projects',require ('./Routers/API/projects'));

app.use('/tasks',require ('./Routers/API/tasks'));

app.use('/activities',require ('./Routers/API/activities'));

app.use('/', router); // apply the routes to our application


// START THE SERVER
// ==============================================
app.listen(port);
console.log('Listening ' + port);

module.exports = {app};

【问题讨论】:

  • 云功能不允许您使用app.listen()。对于 HTTP 类型的函数,基础设施会监听你。您只需为应用程序提供处理传入连接的权限。

标签: javascript express google-cloud-platform google-cloud-functions


【解决方案1】:

就像this post 中提到的,要在 Google Cloud Function 中使用 express,您可以将服务器代码放在主文件中,并将 app 变量设置为入口点。

【讨论】:

    猜你喜欢
    • 2019-08-05
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    • 2020-05-30
    • 1970-01-01
    • 2020-06-04
    • 2017-11-10
    • 2019-09-19
    相关资源
    最近更新 更多