eri*_*icn 12 node.js express google-cloud-functions
我使用以下步骤生成了一个演示 Express 服务器:
npm install -g express-generatorexpress myExpressApp --view pug不用说,该应用程序在我的本地计算机上运行良好(npm start)
然后我将代码推送到云源存储库
然后通过其网络应用程序部署到 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;
Run Code Online (Sandbox Code Playgroud)
/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;
Run Code Online (Sandbox Code Playgroud)
最终更新
我按照 Abrar Hossain app.js 的建议添加了以下内容
//...
module.exports = app;
exports.app = functions.http.onRequest(app);
module.exports = { app };
Run Code Online (Sandbox Code Playgroud)
包.json
"scripts": {
"start": "functions-framework --target=company"
},
"dependencies": {
"@google-cloud/functions-framework": "^1.7.1",
Run Code Online (Sandbox Code Playgroud)
但问题依然存在
简而言之,在部署过程中,您必须指定要用作GCP云函数的JS函数的名称,它是指定的--entry-point标志。更多信息请参见此处(请查看标志部分)。app在你的情况下。如果您提供部署脚本,我可以为您指出确切的位置
长答案 - 常见的误解是 GCP Cloud 功能 = Express 应用程序。它不是。尽管在幕后他们可能会使用express,并且您可能会通过配置请求某些行为。实际上,对于 GCP 来说,它只是一个 JS 函数,仅此而已。没有路由器或类似的东西。如果您想在本地测试它,请使用Functions Framework。您可以在我的帖子中了解更多详细信息
| 归档时间: |
|
| 查看次数: |
16100 次 |
| 最近记录: |