简体   繁体   中英

Site works locally / Not on Openshift (syncing local, global npm package versions with server, global npm package versions)

I am creating a forum with Angular - it interfaces with Node/Mongo, so there are plenty of requests that need to be executed to fill the page with the required data. Everything works exactly as it should locally, but when I upload to Openshift...the site seems to be very buggy, and it doesn't seem to execute more than half of the requests (if not all) or the requests I put in to make the forum function.

I am using a yeoman MEAN stack called angular-fullstack . If you have yeoman you can create the seed with yo angular-fullstack .

The only things I had to change within that seed to get it to upload to openshift were:

server.js

...

// Set default node environment to development
process.env.NODE_ENV = process.env.NODE_ENV || 'development';

...

to

...

// Set default node environment to development
process.env.NODE_ENV = process.env.NODE_ENV || 'production';

...

The above line refers to this code in lib/config/express.js:

if ('development' === env) {
  app.use(require('connect-livereload')());

  // Disable caching of scripts for easier testing
  app.use(function noCache(req, res, next) {
    if (req.url.indexOf('/scripts/') === 0) {
      res.header('Cache-Control', 'no-cache, no-store, must-revalidate');
      res.header('Pragma', 'no-cache');
      res.header('Expires', 0);
    }
    next();
  });

  app.use(express.static(path.join(config.root, '.tmp')));
  app.use(express.static(path.join(config.root, 'app')));
  app.set('views', config.root + '/app/views');
}

if ('production' === env) {
  app.use(compression());
  app.use(errorHandler());
  app.use(favicon(path.join(config.root, 'public', 'favicon.ico')));
  app.use(express.static(path.join(config.root, 'public')));
  app.set('views', config.root + '/views');
}

...

// Error handler - has to be last
if ('development' === app.get('env')) {
  app.use(errorHandler());
}

So let me know if you notice anything wrong with that production chunk of code - that is what is being run.

Also, the Mongo URI needed to be changed to work with the MongoDB cartridge I set up on Openshift:

mongo: {
uri: process.env.MONGOLAB_URI ||
     process.env.MONGOHQ_URL ||
     process.env.OPENSHIFT_MONGODB_DB_URL+process.env.OPENSHIFT_APP_NAME ||
     'mongodb://whateveritwas'
}

to

mongo: {
uri: process.env.MONGOLAB_URI ||
     process.env.MONGOHQ_URL ||
     process.env.OPENSHIFT_MONGODB_DB_URL+process.env.OPENSHIFT_APP_NAME ||
     'mongodb://admin:xxxxx@127.10.13.X:27017/hackabox' //the X's are to protect me
}

Initially to get it to upload to Openshift, you have to run yo angular-fullstack:openshift in the project folder, but I already did this, so now all I have to do to update my code on Openshift is:

  1. Go into the project folder in the console, and type grunt build (this creates the dist folder)

  2. Change into the dist folder, add everything to git and run git push .

After that, I can go to the Openshift URL and see the site.

What is exactly happening now is (what I am focusing on) - is when you go to a page where you can view the post (it's contents, it's author, it's comments) - you can add comments, and they will show up initially because of how angular works...but when I navigate away, and come back...this code doesn't seem to execute as it should:

$scope.loadpost = function() {
  $http.get('/api/posts/'+$routeParams.id).success(function (data) {
    $scope.post = data;
    $scope.comments = $scope.post.comments;
    console.log('this is scope comments ' + $scope.comments);
    if($scope.comments[0] !== undefined) {
      $scope.commentstring.empty = '';
    }
  });
};

For testing purposes...I am calling it at the top of the post controller, like this:

$scope.loadpost();

I am also using ng-init to call it when the page loads, like this:

<div ng-init="loadpost()">

THIS IS WHERE I GO INTO DETAIL ABOUT THE PROBLEM

I know this is redundant...but the point is...the function never gets called when the page loads...and all the post content that it needs to get from the database never arrives, because the request never happens - I can see that nothing happens in the console - and you can too, check out what I am talking about in the "Forum" section of my page - http://hackabox-eamonbenproject.rhcloud.com/forum . To post you will have to sign up as a user...and then you can see what I am talking about by going to the Forum, clicking on one of the posts...making a comment...and then navigating away and coming back...you don't have to make the comment, you will see that nothing shows up...but it will help you see the problem, because when the comment is created, a PUT request is supposed to happen which updates the post, but that never happens either...along with a bunch of GET requests that don't get called too. Also, another thing, I noticed (at least once)...that if I do something in the forum...and then navigate to the "Settings" page, sometimes the logo goes away...

I am starting to think that the minification process that goes on when I run grunt build is messing up the code...so that when it gets loaded to openshift, it becomes really buggy...but this is just a hunch - I guess to test my theory, I was wondering if there was anyway to upload to Openshift without minifying?

Thanks!

UPDATE

As Pogrindis and I discussed this morning...there is an error in the node logs on openshift when a request is supposed to happen. It says in the node logs that it is an Express error...and we realized that the global version for npm on openshift was using 3.2.5, while the seed I am using uses express v4.2.0 (and that is what I use globally, locally). I am in the process of downgrading my local, global express npm to 3.2.5 to see if this corrects the problem...but +1 to Pogrindis for teaching me about Node Version Manager...as well as having to possibly sync the versions of any npm package that is installed globally on Openshift! Will keep everyone posted to make sure this resolves the problem!

Hard question to answer. A lot to look at.

Version-ing

Versions are huge in node, you could have a bad version and sh*t will hit the fan.

There is an exceptional VERSION Saving github topic which comes to mind.

If this is not the issue, you still need to keep in mind that its not a strongly types lang, that includes the versioning.

Each situation is different.

https://github.com/npm/npm/issues/3417

This issue 'FIX' will kill a lot of users who have been using the previous(minor) version. You need to be be specific with versioning. (i use this as an example)

You will hit a lot of version issues with Node, as good as it is, unless you specify the version, it will get the latest version.

So, working on your local (3 months of hard sexy labor) machine means nothing unless in your packages you have a version specified.

Your server will only have the latest version.

From the question.. It can only BE a versioning problem.

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