Google Cloud Functions - what is the entry point for Express js and Node JS 10?

Viewed 3318

My Node JS version is 10

Express JS version: 4.16.1

I know that [the entry point should be "app" for Node JS 8][1]

The generated code (from `npm install -g express-generator) www

#!/usr/bin/env node

/**
 * Module dependencies.
 */

var app = require('../app');
var debug = require('debug')('myexpressapp:server');
var http = require('http');

/**
 * Get port from environment and store in Express.
 */

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
 * Create HTTP server.
 */

var server = http.createServer(app);

/**
 * Listen on provided port, on all network interfaces.
 */

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
 * Normalize a port into a number, string, or false.
 */

function normalizePort(val) {
  var port = parseInt(val, 10);

  if (isNaN(port)) {
    // named pipe
    return val;
  }

  if (port >= 0) {
    // port number
    return port;
  }

  return false;
}

/**
 * Event listener for HTTP server "error" event.
 */

function onError(error) {
  if (error.syscall !== 'listen') {
    throw error;
  }

  var bind = typeof port === 'string'
    ? 'Pipe ' + port
    : 'Port ' + port;

  // handle specific listen errors with friendly messages
  switch (error.code) {
    case 'EACCES':
      console.error(bind + ' requires elevated privileges');
      process.exit(1);
      break;
    case 'EADDRINUSE':
      console.error(bind + ' is already in use');
      process.exit(1);
      break;
    default:
      throw error;
  }
}

/**
 * Event listener for HTTP server "listening" event.
 */

function onListening() {
  var addr = server.address();
  var bind = typeof addr === 'string'
    ? 'pipe ' + addr
    : 'port ' + addr.port;
  debug('Listening on ' + bind);
}

package.json

{
  "name": "myexpressapp",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    "cookie-parser": "~1.4.4",
    "debug": "~2.6.9",
    "express": "~4.16.1",
    "http-errors": "~1.6.3",
    "morgan": "~1.9.1",
    "pug": "2.0.0-beta11"
  }
}

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;

This is Google Cloud Functions' hello world example, which is based on their own Functions Framework: [![enter image description here][2]][2]

This is the view that I have [![enter image description here][3]][3]

Final update Added the following to app.js but it still doesn't work

//...
module.exports = app;

exports.app = functions.http.onRequest(app);
module.exports = { app };

`` Build failed: function.js does not exist; Error ID: 7485c5b6




  [1]: https://stackoverflow.com/questions/57587844/error-could-not-handle-the-request-with-google-cloud-function-and-express/57592171#57592171
  [2]: https://i.stack.imgur.com/WOOaw.png
  [3]: https://i.stack.imgur.com/F52jv.png
  [4]: https://i.stack.imgur.com/qmRe9.png
1 Answers

The entry point is defined during the function deployment. As you can see in the screenshot in the answer you have linked, you have to specify the name of the function. If you are using the console then you have to specify the entry point with --entry-pointflag. More information here.

Two important points to make about your questions:

  1. Whatever settings of express you have absolutely no effect on GCP Cloud Functions. Although it may use Express under the hood, for GCP, your function is a single/simple JS method it will execute when a trigger is activated
  2. Make sure that you make functions available by exporting it, e.g.
module.exports = {
  my_function_name
}
Related