简体   繁体   中英

How to deploy node.js app on Google App Engine so it loads when user goes to app URL?

I'm new to Google App Engine, and have a node.js application that I'm deploying from a GitHub repo. Currently I seem to have to manually enter a number of steps to preview the app, and when I attempt to deploy it with 'gcloud app deploy', the server only throws a 500 error. What I'd like to have happen is have 'gcloud app deploy' actually work. Presumably that means that whether or not I'm logged into Google Cloud Services, going to lucidnodes-dev.appspot.com actually runs the app.

After two days of playing around with dockerfiles, I'm crying uncle. I need help.

The manual steps I'm running are, from inside the Google Cloud Shell:

cd LucidNodes // Enter the directory where the app files live
node index.js

This returns:

LucidNodes listening on port 3000

As is expected, and I can see the app running when I run Web Preview on port 3000.

Here is my server code:

server.js file:

const express = require('express');
const path = require('path');
const url = require('url');

var init = function( app, prt ){

    app.listen( prt, () => console.log('LucidNodes listening on port ' , prt ));
};

exports.init = init;

and index.js file:

const server = require('./server');
const fs = require('fs');
const express = require('express');
const path = require('path');
const serveStatic = require('serve-static');
const serveIndex = require('serve-index');
const staticPaths = require('./defineStaticPaths');
const htmlMethods = require('./js/htmlMethods');  // import custom methods for handling HTML
const app = express();
const routes = require('./routes');

staticPaths.defineStaticPaths( app );
app.use( routes );

server.init( app, 8080 );

Note that I've changed the port on the local and GitHub instances of the app to 8080. I'm not sure if changing the port number would solve the 500 error problem when deploying and running the app, but I also couldn't get the app instance on GAE to update from the GitHub repo, hence the app is still running on port 3000 on GAE, and hence also the digression to dockerfiles, which so far has been yet another learning curve mess, but one thing at a time I suppose.

So... what I'm trying to do is get the app to actually deploy and run when I call up the lucidnodes-dev.appspot.com. How to do that?

Thanks for the help.

When deploying to App Engine, make sure that:

  • there is a well-formed and valid app.yaml file in the root directory of your app
  • the command glcoud app deploy is ran from the same root directory
  • package.json includes the script field, instructing App Engine to install dependencies and run the web server:

"scripts": {
  "start": "node index.js"
}

If you don't include this in package.json , the app keeps trying to deploy on a local server, which of course doesn't work. The fact that you are seeing your website anyway is most probably due to cached versions of the app, or previous versions which still run after a new failed deployment.

Are you deploying in Standard or Flexible environment?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM