node.js - Google Cloud Functions 构建失败:function.js 不存在;错误 ID:7485c5b6
问题描述
我使用以下步骤生成了一个演示快速服务器:
npm install -g express-generator
express myExpressApp --view pug
不用说,该应用程序在我的本地机器上运行良好 ( npm start
)
然后我将代码推送到 Cloud Source Repositories
然后通过他们的网络应用部署到 Google Cloud Functions
我生成的演示应用程序的源代码:
/myExpressApp/app.js
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
/myExpressApp/routes/index.js
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
module.exports = router;
最终更新
我按照 Abrar Hossain app.js 的建议添加了以下内容
//...
module.exports = app;
exports.app = functions.http.onRequest(app);
module.exports = { app };
包.json
"scripts": {
"start": "functions-framework --target=company"
},
"dependencies": {
"@google-cloud/functions-framework": "^1.7.1",
但问题依然存在
解决方案
简而言之,在部署过程中,你必须指定你想要作为 GCP Cloud 函数使用的 JS 函数的名称,它是指定--entry-point
标志。更多信息在这里(请查看标志部分)。app
在你的情况下。如果您提供部署脚本,我可以为您指出确切的位置
长答案 - 常见的误解是 GCP Cloud 功能 = Express 应用程序。它不是。尽管在幕后他们可能会使用 express 并且您可能会通过配置请求某些行为。实际上,对于 GCP 来说,它只是一个 JS 函数,不多也不少。没有路由器或类似的东西。如果您想在本地测试它,请使用Functions Framework。您可以在我的帖子中获得更多详细信息
推荐阅读
- javascript - 使用 React Bootstrap 自定义 Card.Header
- react-native - 如何使用 useState 挂钩更新对象
- c# - 我可以使用 Environment.Username 和 MYSQL 来验证登录吗?
- python - How to check if a PNG has a certain RGB color using Python?
- python-3.x - 如何在 html 表中抓取?
- airflow - gcloud composer command fails "executable file not found in $PATH"
- ios - 能够在 IOS 中使用的转换器条带
- javascript - How can I call a variable outside the widget
- python - find peaks and envelope question in python
- reactjs - 将路线直接反应到同一路线不更新