Node.js 如何在一个存储库中编写多个Google云函数
我需要在GoogleCloud中部署多个函数,使用一个存储库(来自GoogleCloud)并在NodeJS中使用Express 有可能做到这一点吗 我有两个不同的模块(结帐、客户)和一个索引: checkout.jsNode.js 如何在一个存储库中编写多个Google云函数,node.js,express,google-cloud-functions,Node.js,Express,Google Cloud Functions,我需要在GoogleCloud中部署多个函数,使用一个存储库(来自GoogleCloud)并在NodeJS中使用Express 有可能做到这一点吗 我有两个不同的模块(结帐、客户)和一个索引: checkout.js /** * Responds to any HTTP request. * * @param {!express:Request} req HTTP request context. * @param {!express:Response} res HTTP respons
/**
* Responds to any HTTP request.
*
* @param {!express:Request} req HTTP request context.
* @param {!express:Response} res HTTP response context.
*
*
*/
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const checkout = express();
checkout.use(cors({
origin: '*'
}));
const PORT = 5555;
checkout.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
const COMMENTS = [
{
id: 1,
firstName: 'John1',
lastName: 'Smith'
},
{
id: 2,
firstName: 'Jane',
lastName: 'Williams'
}
];
checkout.get('/comments', (req, res, next) => {
res.json(process.env.TEST || 33);
}, function (err, req, res, next) {
res.json(err);
});
checkout.get('/:commentId', (req, res, next) => {
res.json(COMMENTS.find(comment => comment.id === parseInt(req.params.commentId)));
});
module.exports = checkout;
/**
* Responds to any HTTP request.
*
* @param {!express:Request} req HTTP request context.
* @param {!express:Response} res HTTP response context.
*
*
*/
const express = require('express');
const cors = require('cors');
const customer = express();
customer.use(cors({
origin: '*'
}));
const PORT = 5555;
customer.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
const USERS = [
{
id: 1,
firstName: 'John',
lastName: 'Smith'
},
{
id: 2,
firstName: 'Jane',
lastName: 'Williams'
}
];
customer.get('/users', (req, res, next) => {
res.json(USERS);
});
customer.get('/:userId', (req, res, next) => {
res.json(USERS.find(user => user.id === parseInt(req.params.userId)));
});
module.exports = customer;
customer.js
/**
* Responds to any HTTP request.
*
* @param {!express:Request} req HTTP request context.
* @param {!express:Response} res HTTP response context.
*
*
*/
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const checkout = express();
checkout.use(cors({
origin: '*'
}));
const PORT = 5555;
checkout.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
const COMMENTS = [
{
id: 1,
firstName: 'John1',
lastName: 'Smith'
},
{
id: 2,
firstName: 'Jane',
lastName: 'Williams'
}
];
checkout.get('/comments', (req, res, next) => {
res.json(process.env.TEST || 33);
}, function (err, req, res, next) {
res.json(err);
});
checkout.get('/:commentId', (req, res, next) => {
res.json(COMMENTS.find(comment => comment.id === parseInt(req.params.commentId)));
});
module.exports = checkout;
/**
* Responds to any HTTP request.
*
* @param {!express:Request} req HTTP request context.
* @param {!express:Response} res HTTP response context.
*
*
*/
const express = require('express');
const cors = require('cors');
const customer = express();
customer.use(cors({
origin: '*'
}));
const PORT = 5555;
customer.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
const USERS = [
{
id: 1,
firstName: 'John',
lastName: 'Smith'
},
{
id: 2,
firstName: 'Jane',
lastName: 'Williams'
}
];
customer.get('/users', (req, res, next) => {
res.json(USERS);
});
customer.get('/:userId', (req, res, next) => {
res.json(USERS.find(user => user.id === parseInt(req.params.userId)));
});
module.exports = customer;
如何在inde.js中导入这些模块
如果我这样添加,函数不会返回响应:
const checkout = require('./checkout');
const customer = require('./customer');
module.require = {
checkout,
customer
};
导入需要部署到index.js的所有函数 然后导出每个函数
您导出的每个函数都将部署到云函数。是的,但我如何才能做到这一点?我添加了更多的代码来解释。无论您如何导出东西,这都不会起作用。您不能在云函数中侦听端口。那部分是为你管理的。您所能部署的就是您的路由。@DougStevenson我如何为不同的google云功能导出不同的路由?