1
我在google雲上有一個f1-micro實例。 IN安裝了ubuntu 14.04,NodejS 0.10和mongoDB。現在我已經在yeoman上做了一個明確的應用,它在本地主機上完美運行。但是當我嘗試在實例中運行它時,我無法訪問它!禁止訪問Google Compute Engine上託管的NodeJS Express應用程序
下面是我做的:
- 提交本地代碼通過SSH來到位桶從到位桶
- 克隆代碼,谷歌計算引擎
- 運行命令咕嚕
- 訪問所提供的外部IP由谷歌與端口號。在瀏覽器,但它說:此網頁無法使用
這裏是我的源代碼:
** ** 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' ]);
};
我試着改變app.listen與谷歌提供的內部IP,但仍然沒有運氣。有任何想法嗎?
目前爲止,我將端口更改爲80,看起來像是有效。不過,我想使用端口3000,並不知道在谷歌雲控制檯中的任何方式 –