sahat / hackathon-starter

A boilerplate for Node.js web applications
MIT License
34.83k stars 8.16k forks source link

Openshift port error #567

Closed Carino-Technologies closed 8 years ago

Carino-Technologies commented 8 years ago

Hi, I'm cloning your stater app on openshift but continuously i got error i already do deployment points but still having error.


remote: Preparing build for deployment
remote: Deployment id is d3588f75
remote: Activating deployment
remote: Starting NodeJS cartridge
remote: Mon Sep 12 2016 15:45:53 GMT-0400 (EDT): Starting application 'woo' ...
remote: Waiting for application port (8080) become available ...
remote: Application 'woo' failed to start (port 8080 not available)
remote: -------------------------
remote: Git Post-Receive Result: failure
remote: Activation status: failure
remote: Activation failed for the following gears:
remote: 57d702820c1e66a4af000165 (Error activating gear: CLIENT_ERROR: Failed to
 execute: 'control start' for /var/lib/openshift/57d702820c1e66a4af000165/nodejs

remote: #<IO:0x000000009853a0>
remote: #<IO:0x00000000985328>
remote: )
remote: Deployment completed with status: failure
remote: postreceive failed
To ssh://woo-wpsaad.rhcloud.com/~/git/woo.git/
   522391e..a10190c  master -> master
Carino-Technologies commented 8 years ago

Error while restarting the server (node js)

Stopping NodeJS cartridge
Mon Sep 12 2016 16:00:35 GMT-0400 (EDT): Stopping application 'woo' ...
Mon Sep 12 2016 16:00:36 GMT-0400 (EDT): Stopped Node application 'woo'
Starting NodeJS cartridge
Mon Sep 12 2016 16:00:37 GMT-0400 (EDT): Starting application 'woo' ...
Waiting for application port (8080) become available ...
Application 'woo' failed to start (port 8080 not available)
Failed to execute: 'control restart' for /var/lib/openshift/57d702820c1e66a4af000165/nodejs
sahat commented 8 years ago

@Carino-Technologies Did you change app.set('port') to use OPENSHIFT_PORT environment variable?

Carino-Technologies commented 8 years ago

@sahat Yes, as you written in the documentation i have set the env port of node js but not change the default setting of app.set('port') i have just leave it as i downloaded from your code. I have just shared my app.js file below

/**
 * Module dependencies.
 */
const express = require('express');
const compression = require('compression');
const session = require('express-session');
const bodyParser = require('body-parser');
const logger = require('morgan');
const chalk = require('chalk');
const errorHandler = require('errorhandler');
const lusca = require('lusca');
const dotenv = require('dotenv');
const MongoStore = require('connect-mongo')(session);
const flash = require('express-flash');
const path = require('path');
const mongoose = require('mongoose');
const passport = require('passport');
const expressValidator = require('express-validator');
const sass = require('node-sass-middleware');
const multer = require('multer');
const upload = multer({ dest: path.join(__dirname, 'uploads') });

/**
 * Load environment variables from .env file, where API keys and passwords are configured.
 */
dotenv.load({ path: '.env.example' });

/**
 * Controllers (route handlers).
 */
const homeController = require('./controllers/home');
const userController = require('./controllers/user');
const apiController = require('./controllers/api');
const contactController = require('./controllers/contact');

/**
 * API keys and Passport configuration.
 */
const passportConfig = require('./config/passport');

/**
 * Create Express server.
 */
const app = express();

/**
 * Connect to MongoDB.
 */
mongoose.connect(process.env.MONGODB_URI || process.env.MONGOLAB_URI);
mongoose.connection.on('connected', () => {
  console.log('%s MongoDB connection established!', chalk.green('✓'));
});
mongoose.connection.on('error', () => {
  console.log('%s MongoDB connection error. Please make sure MongoDB is running.', chalk.red('✗'));
  process.exit();
});

/**
 * Express configuration.
 */
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(compression());
app.use(sass({
  src: path.join(__dirname, 'public'),
  dest: path.join(__dirname, 'public')
}));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(expressValidator());
app.use(session({
  resave: true,
  saveUninitialized: true,
  secret: process.env.SESSION_SECRET,
  store: new MongoStore({
    url: process.env.MONGODB_URI || process.env.MONGOLAB_URI,
    autoReconnect: true
  })
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
app.use((req, res, next) => {
  if (req.path === '/api/upload') {
    next();
  } else {
    lusca.csrf()(req, res, next);
  }
});
app.use(lusca.xframe('SAMEORIGIN'));
app.use(lusca.xssProtection(true));
app.use((req, res, next) => {
  res.locals.user = req.user;
  next();
});
app.use(function(req, res, next) {
  // After successful login, redirect back to the intended page
  if (!req.user &&
      req.path !== '/login' &&
      req.path !== '/signup' &&
      !req.path.match(/^\/auth/) &&
      !req.path.match(/\./)) {
    req.session.returnTo = req.path;
  }
  next();
});
app.use(express.static(path.join(__dirname, 'public'), { maxAge: 31557600000 }));

/**
 * Primary app routes.
 */
app.get('/', homeController.index);
app.get('/login', userController.getLogin);
app.post('/login', userController.postLogin);
app.get('/logout', userController.logout);
app.get('/forgot', userController.getForgot);
app.post('/forgot', userController.postForgot);
app.get('/reset/:token', userController.getReset);
app.post('/reset/:token', userController.postReset);
app.get('/signup', userController.getSignup);
app.post('/signup', userController.postSignup);
app.get('/contact', contactController.getContact);
app.post('/contact', contactController.postContact);
app.get('/account', passportConfig.isAuthenticated, userController.getAccount);
app.post('/account/profile', passportConfig.isAuthenticated, userController.postUpdateProfile);
app.post('/account/password', passportConfig.isAuthenticated, userController.postUpdatePassword);
app.post('/account/delete', passportConfig.isAuthenticated, userController.postDeleteAccount);
app.get('/account/unlink/:provider', passportConfig.isAuthenticated, userController.getOauthUnlink);

/**
 * API examples routes.
 */
app.get('/api', apiController.getApi);
app.get('/api/lastfm', apiController.getLastfm);
app.get('/api/nyt', apiController.getNewYorkTimes);
app.get('/api/aviary', apiController.getAviary);
app.get('/api/steam', passportConfig.isAuthenticated, passportConfig.isAuthorized, apiController.getSteam);
app.get('/api/stripe', apiController.getStripe);
app.post('/api/stripe', apiController.postStripe);
app.get('/api/scraping', apiController.getScraping);
app.get('/api/twilio', apiController.getTwilio);
app.post('/api/twilio', apiController.postTwilio);
app.get('/api/clockwork', apiController.getClockwork);
app.post('/api/clockwork', apiController.postClockwork);
app.get('/api/foursquare', passportConfig.isAuthenticated, passportConfig.isAuthorized, apiController.getFoursquare);
app.get('/api/tumblr', passportConfig.isAuthenticated, passportConfig.isAuthorized, apiController.getTumblr);
app.get('/api/facebook', passportConfig.isAuthenticated, passportConfig.isAuthorized, apiController.getFacebook);
app.get('/api/github', passportConfig.isAuthenticated, passportConfig.isAuthorized, apiController.getGithub);
app.get('/api/twitter', passportConfig.isAuthenticated, passportConfig.isAuthorized, apiController.getTwitter);
app.post('/api/twitter', passportConfig.isAuthenticated, passportConfig.isAuthorized, apiController.postTwitter);
app.get('/api/linkedin', passportConfig.isAuthenticated, passportConfig.isAuthorized, apiController.getLinkedin);
app.get('/api/instagram', passportConfig.isAuthenticated, passportConfig.isAuthorized, apiController.getInstagram);
app.get('/api/paypal', apiController.getPayPal);
app.get('/api/paypal/success', apiController.getPayPalSuccess);
app.get('/api/paypal/cancel', apiController.getPayPalCancel);
app.get('/api/lob', apiController.getLob);
app.get('/api/upload', apiController.getFileUpload);
app.post('/api/upload', upload.single('myFile'), apiController.postFileUpload);
app.get('/api/pinterest', passportConfig.isAuthenticated, passportConfig.isAuthorized, apiController.getPinterest);
app.post('/api/pinterest', passportConfig.isAuthenticated, passportConfig.isAuthorized, apiController.postPinterest);
app.get('/api/google-maps', apiController.getGoogleMaps);

/**
 * OAuth authentication routes. (Sign in)
 */
app.get('/auth/instagram', passport.authenticate('instagram'));
app.get('/auth/instagram/callback', passport.authenticate('instagram', { failureRedirect: '/login' }), (req, res) => {
  res.redirect(req.session.returnTo || '/');
});
app.get('/auth/facebook', passport.authenticate('facebook', { scope: ['email', 'user_location'] }));
app.get('/auth/facebook/callback', passport.authenticate('facebook', { failureRedirect: '/login' }), (req, res) => {
  res.redirect(req.session.returnTo || '/');
});
app.get('/auth/github', passport.authenticate('github'));
app.get('/auth/github/callback', passport.authenticate('github', { failureRedirect: '/login' }), (req, res) => {
  res.redirect(req.session.returnTo || '/');
});
app.get('/auth/google', passport.authenticate('google', { scope: 'profile email' }));
app.get('/auth/google/callback', passport.authenticate('google', { failureRedirect: '/login' }), (req, res) => {
  res.redirect(req.session.returnTo || '/');
});
app.get('/auth/twitter', passport.authenticate('twitter'));
app.get('/auth/twitter/callback', passport.authenticate('twitter', { failureRedirect: '/login' }), (req, res) => {
  res.redirect(req.session.returnTo || '/');
});
app.get('/auth/linkedin', passport.authenticate('linkedin', { state: 'SOME STATE' }));
app.get('/auth/linkedin/callback', passport.authenticate('linkedin', { failureRedirect: '/login' }), (req, res) => {
  res.redirect(req.session.returnTo || '/');
});

/**
 * OAuth authorization routes. (API examples)
 */
app.get('/auth/foursquare', passport.authorize('foursquare'));
app.get('/auth/foursquare/callback', passport.authorize('foursquare', { failureRedirect: '/api' }), (req, res) => {
  res.redirect('/api/foursquare');
});
app.get('/auth/tumblr', passport.authorize('tumblr'));
app.get('/auth/tumblr/callback', passport.authorize('tumblr', { failureRedirect: '/api' }), (req, res) => {
  res.redirect('/api/tumblr');
});
app.get('/auth/steam', passport.authorize('openid', { state: 'SOME STATE' }));
app.get('/auth/steam/callback', passport.authorize('openid', { failureRedirect: '/login' }), (req, res) => {
  res.redirect(req.session.returnTo || '/');
});
app.get('/auth/pinterest', passport.authorize('pinterest', { scope: 'read_public write_public' }));
app.get('/auth/pinterest/callback', passport.authorize('pinterest', { failureRedirect: '/login' }), (req, res) => {
  res.redirect('/api/pinterest');
});

/**
 * Error Handler.
 */
app.use(errorHandler());

/**
 * Start Express server.
 */
var IP_ADDRESS = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1';
var PORT = process.env.OPENSHIFT_NODEJS_PORT || 8080;

app.listen(PORT, IP_ADDRESS,() => {
  console.log(`Express server listening on port ${PORT} in ${app.settings.env} mode`);
});

module.exports = app;
Carino-Technologies commented 8 years ago

@sahat and here is the code how i connect mongo db,

MONGODB_URI=mongodb://cross-user:********@ds029476.mlab.com:29476/cross
MONGOLAB_URI=mongodb://cross-user:********@ds029476.mlab.com:29476/cross
sahat commented 8 years ago

Take a look at my settings for the live demo app running on OpenShift: https://github.com/sahat/hackathon-starter/blob/demo/app.js

Not sure if this will fix it, but try:

app.set('host', process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0');
app.set('port', process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080);
app.listen(app.get('port'), app.get('host'), () => {
  console.log('Express server listening on port %d in %s mode', app.get('port'), app.get('env'));
});
Carino-Technologies commented 8 years ago

No, Not working, i just clone your demo branch but still got the same errors, i have add a --websocket-port according to article but still same result even i set the port according to this article Link but still same error i have just check with ssh my node is not working.

This is i received on building

remote: Activating deployment
remote: Starting MongoDB cartridge
remote: Waiting for mongo to start...
remote: Starting RockMongo cartridge
remote: Starting NodeJS cartridge
remote: Thu Sep 15 2016 11:47:55 GMT-0400 (EDT): Starting application 'woo' ...
remote: Waiting for application port (8080) become available ...
remote: Application 'woo' failed to start (port 8080 not available)
remote: -------------------------
remote: Git Post-Receive Result: failure
remote: Activation status: failure
remote: Activation failed for the following gears:
remote: 57d702820c1e66a4af000165 (Error activating gear: CLIENT_ERROR: Failed to
 execute: 'control start' for /var/lib/openshift/57d702820c1e66a4af000165/nodejs

remote: #<IO:0x000000010e1388>
remote: #<IO:0x000000010e1310>
remote: )
remote: Deployment completed with status: failure
remote: postreceive failed
To ssh://woo-wpsaad.rhcloud.com/~/git/woo.git/
   a623470..44e97fa  master -> master

Here is the log of nodejs

at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:929:3
DEBUG: Program node app.js exited with code 8
DEBUG: Starting child process with 'node app.js'
/var/lib/openshift/57d702820c1e66a4af000165/app-root/runtime/repo/app.js:50
mongoose.connection.on('connected', () => {
                                     ^
SyntaxError: Unexpected token )
    at Module._compile (module.js:439:25)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:929:3
DEBUG: Program node app.js exited with code 8
DEBUG: Starting child process with 'node app.js'
/var/lib/openshift/57d702820c1e66a4af000165/app-root/runtime/repo/app.js:50
mongoose.connection.on('connected', () => {
                                     ^
SyntaxError: Unexpected token )
    at Module._compile (module.js:439:25)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:929:3
sahat commented 8 years ago

It's failing because the Node.js version on OpenShift doesn't support ES6 syntax.

I remember now, I had to add .openshift folder with a file specifying custom Node.js 6.x version. Here: https://github.com/sahat/hackathon-starter/blob/demo/.openshift/markers/NODE_VERSION_URL