Reputation: 2211
I need to deploy multiple functions in Google Cloud Functions with one repository (from Google Cloud) and using Express in NodeJS.
Is possible to achieve this?
I have two different modules (checkout, customer) and an index:
checkout.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;
customer.js
/**
* 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;
How i can import those module in inde.js?
If i add like this the function doesn't return the response:
const checkout = require('./checkout');
const customer = require('./customer');
module.require = {
checkout,
customer
};
Upvotes: 0
Views: 582
Reputation: 158
Import all functions you need to deploy to index.js Then export each function.
Each function you export will be deployed to cloud functions.
Upvotes: 1