Holo
Holo

Reputation: 57

App is not defined in google cloud function with express

I'm trying to use google cloud function to make some API calls. When I deploy on google function, the site send me an Error : 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

What could be the reason for this?

These are the files:

//app.js

exports.simple = app;

App.js should call the google cloud function that execute the function call "simple" IMAGE : 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};

Upvotes: 1

Views: 846

Answers (1)

pessolato
pessolato

Reputation: 1562

Like mentioned on this post, to use express in a Google Cloud Function, you can put your server code inside the main file and set the app variable as the entry point.

Upvotes: 2

Related Questions