最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - No Access to NodeJS Express App hosted on Google Compute Engine - Stack Overflow

programmeradmin0浏览0评论

I have a f1-micro instance on google cloud. IN installed ubuntu 14.04, NodejS 0.10 and mongoDB. Now I have made an express application with yeoman which works perfectly on localhost. But when I try to run it in the instance, I can not access it!

Here' what I do:

  1. Commit local code to BitBucket
  2. Clone code from BitBUcket to Google Compute Engine through SSH
  3. run mand grunt
  4. Access the external IP provided by Google with port no. on browser but it says This webpage is not available

Here is my source code:

** app.js **

'use strict';

// Module dependencies.
var express = require('express'),
    path = require('path'),
    fs = require('fs'),
    methodOverride = require('method-override'),
    morgan = require('morgan'),
    bodyParser = require('body-parser'),
    errorhandler = require('errorhandler');

var app = module.exports = exports.app = express();

app.locals.siteName = "Server";

// Connect to database
var db = require('./config/db');
app.use(express.static(__dirname + '/public'));


// Bootstrap models
var modelsPath = path.join(__dirname, 'models');
fs.readdirSync(modelsPath).forEach(function (file) {
  require(modelsPath + '/' + file);
});

var env = process.env.NODE_ENV || 'development';

if ('development' == env) {
    app.use(morgan('dev'));
    app.use(errorhandler({
        dumpExceptions: true,
        showStack: true
    }));
    app.set('view options', {
        pretty: true
    });
}

if ('test' == env) {
    app.use(morgan('test'));
    app.set('view options', {
        pretty: true
    });
    app.use(errorhandler({
        dumpExceptions: true,
        showStack: true
    }));
}

if ('production' == env) {
    app.use(morgan());
     app.use(errorhandler({
        dumpExceptions: false,
        showStack: false
    }));
}

app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(methodOverride());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

// Bootstrap routes
var routesPath = path.join(__dirname, 'routes');
fs.readdirSync(routesPath).forEach(function(file) {
  app.use('/', require(routesPath + '/' + file));
});

// Bootstrap api
var apiPath = path.join(__dirname, 'api');
fs.readdirSync(apiPath).forEach(function(file) {
  app.use('/api', require(apiPath + '/' + file));
});

// Start server
var port = process.env.PORT || 3000;
app.listen(port, function () {
  console.log('Express server listening on port %d in %s mode', port, app.get('env'));
});

** Gruntfile.js **

module.exports = function(grunt) {
  require('load-grunt-tasks')(grunt);
  grunt.initConfig({
    // Configure a mochaTest task
    mochaTest: {
      test: {
        options: {
          reporter: 'spec'
        },
        src: ['test/**/*.js']
      }
    },
    watch: {
      options: {
        livereload: true,
      },
      express: {
        files:  [ '*.js','routes/*.js', 'models/*.js', 'config/*.js','api/*.js'  ],
        tasks:  [ 'express:dev' ],
        options: {
          spawn: false // Without this option specified express won't be reloaded
        }
      }
    },
    express: {
      options: {
        port : 3000,
        node_env: 'development'
      },
      dev: {
        options: {
          script: 'app.js',
          node_env: 'development'
        }
      },
      prod: {
        options: {
          script: 'app.js',
          node_env: 'production'
        }
      }
    },
    open: {
      server: {
        url: 'http://localhost:3000' 
      }
    }
  });

  grunt.registerTask('test', 'mochaTest');

  grunt.registerTask('server', function(arg) {
    if(arg && arg == 'prod')
    {
      grunt.task.run([
        'express:prod',
        'open',
        'watch'
      ]);
    }
    else
    {
      grunt.task.run([
        'express:dev',
        'open',
        'watch'
      ]);
    }
  });
  grunt.registerTask('default', [ 'server' ]);
  grunt.registerTask('dist', [ 'server:prod' ]);

};

I tried changing the app.listen with Google provided Internal IP but still no luck. Any ideas?

I have a f1-micro instance on google cloud. IN installed ubuntu 14.04, NodejS 0.10 and mongoDB. Now I have made an express application with yeoman which works perfectly on localhost. But when I try to run it in the instance, I can not access it!

Here' what I do:

  1. Commit local code to BitBucket
  2. Clone code from BitBUcket to Google Compute Engine through SSH
  3. run mand grunt
  4. Access the external IP provided by Google with port no. on browser but it says This webpage is not available

Here is my source code:

** app.js **

'use strict';

// Module dependencies.
var express = require('express'),
    path = require('path'),
    fs = require('fs'),
    methodOverride = require('method-override'),
    morgan = require('morgan'),
    bodyParser = require('body-parser'),
    errorhandler = require('errorhandler');

var app = module.exports = exports.app = express();

app.locals.siteName = "Server";

// Connect to database
var db = require('./config/db');
app.use(express.static(__dirname + '/public'));


// Bootstrap models
var modelsPath = path.join(__dirname, 'models');
fs.readdirSync(modelsPath).forEach(function (file) {
  require(modelsPath + '/' + file);
});

var env = process.env.NODE_ENV || 'development';

if ('development' == env) {
    app.use(morgan('dev'));
    app.use(errorhandler({
        dumpExceptions: true,
        showStack: true
    }));
    app.set('view options', {
        pretty: true
    });
}

if ('test' == env) {
    app.use(morgan('test'));
    app.set('view options', {
        pretty: true
    });
    app.use(errorhandler({
        dumpExceptions: true,
        showStack: true
    }));
}

if ('production' == env) {
    app.use(morgan());
     app.use(errorhandler({
        dumpExceptions: false,
        showStack: false
    }));
}

app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(methodOverride());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

// Bootstrap routes
var routesPath = path.join(__dirname, 'routes');
fs.readdirSync(routesPath).forEach(function(file) {
  app.use('/', require(routesPath + '/' + file));
});

// Bootstrap api
var apiPath = path.join(__dirname, 'api');
fs.readdirSync(apiPath).forEach(function(file) {
  app.use('/api', require(apiPath + '/' + file));
});

// Start server
var port = process.env.PORT || 3000;
app.listen(port, function () {
  console.log('Express server listening on port %d in %s mode', port, app.get('env'));
});

** Gruntfile.js **

module.exports = function(grunt) {
  require('load-grunt-tasks')(grunt);
  grunt.initConfig({
    // Configure a mochaTest task
    mochaTest: {
      test: {
        options: {
          reporter: 'spec'
        },
        src: ['test/**/*.js']
      }
    },
    watch: {
      options: {
        livereload: true,
      },
      express: {
        files:  [ '*.js','routes/*.js', 'models/*.js', 'config/*.js','api/*.js'  ],
        tasks:  [ 'express:dev' ],
        options: {
          spawn: false // Without this option specified express won't be reloaded
        }
      }
    },
    express: {
      options: {
        port : 3000,
        node_env: 'development'
      },
      dev: {
        options: {
          script: 'app.js',
          node_env: 'development'
        }
      },
      prod: {
        options: {
          script: 'app.js',
          node_env: 'production'
        }
      }
    },
    open: {
      server: {
        url: 'http://localhost:3000' 
      }
    }
  });

  grunt.registerTask('test', 'mochaTest');

  grunt.registerTask('server', function(arg) {
    if(arg && arg == 'prod')
    {
      grunt.task.run([
        'express:prod',
        'open',
        'watch'
      ]);
    }
    else
    {
      grunt.task.run([
        'express:dev',
        'open',
        'watch'
      ]);
    }
  });
  grunt.registerTask('default', [ 'server' ]);
  grunt.registerTask('dist', [ 'server:prod' ]);

};

I tried changing the app.listen with Google provided Internal IP but still no luck. Any ideas?

Share asked Sep 26, 2015 at 6:02 Abhishek DebAbhishek Deb 8931 gold badge11 silver badges24 bronze badges 1
  • OK so far, I changed the port to 80 and looks like it works. However I would want to use port 3000 and don't know any way of aloowing it in the google cloud console – Abhishek Deb Commented Sep 27, 2015 at 2:37
Add a ment  | 

1 Answer 1

Reset to default 11

Okay so I just figured it out. It was actually very simple. The problem was with the port only. I just needed to change the firewall setting to allow the ports from any source. that's it.

  1. Go to Google Cloud console.
  2. Networking > Firewall Rules.
  3. Find the entry "default-allow-internal" with tcp and udp ranges 1-65535 . Edit it and change the source filter to allow it from any source.
    1. Restart the app with. It should work now.

UPDATE 27 April 2017 : The flow is changed little bit now on GCE. Edit the source filter -> ip range to 0.0.0.0/0

发布评论

评论列表(0)

  1. 暂无评论