DEVGET.NET Blog

How to deploy an Angular 6 project to Heroku 9

How to deploy an Angular 6 project to Heroku

As I had difficulty finding a reliable source online, I decided to create my own.

Here is how you deploy an Angular 6 app to Heroku

Step 1

You are going to need something to serve your files. Let’s go with express. We will also need ‘path’ to setup our server (unless you wanna hardcode those in yourself)

npm install --save express path

Step 2.

Now if we want Heroku to build our project on their servers we need to tell them two things.
1. How to build our project and
2. What versions of node/npm our code works with

You can do this by putting the following in package.json

  "scripts": {
    ...
    "postinstall": "ng build --prod"
  },
  "engines": {
    "node": "8.11.3",
    "npm": "6.1.0"
  },

Remember to replace the versions of node and npm to the ones you have.
You can find out with

node --version
npm --version

Step 3

By default angular separates away from deployments what it thinks are ‘development’ only additions. However, since Heroku is building our code, we need to give it the ability to run those modules.

To do this you can either move @angular/cli, @angular/compiler-cli, typescript and "@angular-devkit/build-angular": "~0.6.8"__*__ from our devDependencies over to dependencies. Or we can have Heroku install those modules on their own.

I personally prefer the first as it allows you to specify versions, however if you wish to do the latter, you would place the following right under postinstall

    "preinstall": "npm install -g @angular/cli @angular/compiler-cli typescript",

Step 4

Create our server file. In your main application directory (the one with package.json) create a file called server.js. Add the following

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

// Serve static files
app.use(express.static(__dirname + '/dist/MY_APP_NAME'));

// Send all requests to index.html
app.get('/*', function(req, res) {
  res.sendFile(path.join(__dirname + '/dist/MY_APP_NAME/index.html'));
});

// default Heroku port
app.listen(process.env.PORT || 5000);

Remember to replace MY_APP_NAME (both of them) to the name of your app.

Step 5

Now to create a Procfile to tell Heroku “how” we wish our app to be run. In your project directory (same one with package.json) create a file called Procfile and place the following

web: node server.js

Step 6. Final Step

We can now build our app with npm install and run it with ‘node server.js’.
If everything works, we should now see a working site on http://localhost:5000

If you have any issues feel free to leave a message in the comments.

To push to heroku, assuming you have the cli installed.
If not (https://devcenter.heroku.com/articles/heroku-cli#download-and-install)

heroku create
git add .
git commit -m "initial heroku deploy'
git push heroku master

Done. You should now see a deploy link. Open it up and you should see your site.

Hope that helped, if you have any issues try typing heroku local on your machine for some more insight.

..

..

..

Still here?

Fine fine.. you’ve convinced me.. here’s a bonus.
Seeing as Heroku employs magical unicorns and it https all the things. You can add this to your server.js to perform a protocol redirect.

// Heroku automagically gives us SSL
// Lets write some middleware to redirect us
let env = process.env.NODE_ENV || 'development';

let forceSSL = (req, res, next) => {
  if (req.headers['x-forwarded-proto'] !== 'https') {
    return res.redirect(['https://', req.get('Host'), req.url].join(''));
  }
  return next();
};

if (env === 'production') {
  app.use(forceSSL);
}

Be sure to place it directly under const app = express() otherwise urls like index.html will not redirect.

0

Ubuntu 18.04 Tools and Apps

This serves to be an ever-changing personal list of what to install if I ever redo my Ubuntu machine. Currently I am running Ubuntu 18.04 so can only guarantee compatibility with that version.

Uncommon

Comfortable Swipehttps://github.com/Hikari9/comfortable-swipe-ubuntu
Lets you map trackpad gestures to just about anything. Also comes with sensible macbook-like defaults

MellowPlayer – https://github.com/ColinDuquesnoy/MellowPlayer
Ever wanted a well integrated linux app for your music player of choice? Deezer? YouTube?
(Note: You will need to install pepperflash)

###Common
Docker
Skype
gnome-tweaks
IntelliJ everything
Visual Studio Code

Extensions

Dash To Dock
Workspace Grid
Sound Input and Output Device chooser

Note: All extensions can either be downloaded from the gnome-extensions site or the integrated software store

How to create an observable in Ionic v2 0

How to create an observable in Ionic v2

Observable.create(observer => {
  this.http.get(url, {headers}).subscribe(res => {
      this.response = res;
      observer.next(this.response);
      observer.complete();
    },
    err => {
      console.log(err);
      observer.error(err);
      observer.complete();
    });
});
How to fix django cors issue 0

How to fix django cors issue

create a file middleware.py and type

class CORSMiddleware:

    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        response = self.get_response(request)
        response['Access-Control-Allow-Origin'] = "*"
        return response

In settings.py

add

MIDDLEWARE = [
    ...
    'product.middleware.CORSMiddleware',
]
Fix: Android emulators not starting in Ubuntu 2

Fix: Android emulators not starting in Ubuntu

Often this happens when for some reason or the other your OpenGL config is broken. This is often an issue on Ubuntu 17.04.

To confirm this is your problem, (Assuming your Android SDK lives in ~/Android/Sdk)

Step 1
Get your emulator’s name:
~/Android/Sdk/emulator/emulator -list-avds

In my case it was Nexus_4_API_25

Step 2
Test running using system OpenGL
~/Android/Sdk/emulator/emulator -avd Nexus_4_API_25 -use-system-libs

If the emulator starts successfully we can make this solution semi-permanent by having a symbolic link in the android Sdk dirs to the system’s openGL

Step 3
cd ~/Android/Sdk/emulator/lib64/libstdc++/
mkdir backup
mv libstdc++.so.6 backup/
ln -sf /usr/lib/x86_64-linux-gnu/libstdc++.so.6 .

Voilà, you should now be able to launch your emulator from android studio or anywhere else. In the case of any future updates, you can just run the above commands again.

 

Source: Martin Revert (stackoverflow)

How to fix gradle in Intellij IDEA 0

How to fix gradle in Intellij IDEA

When starting a project in Intellij IDEA if you see an error around the lines of

Gradle sync failed: The newly created daemon process has a different context than expected.
 It won't be possible to reconnect to this daemon. Context mismatch: 
 Java home is different.
 Wanted: DefaultDaemonContext[uid=null,javaHome=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2016.3.2\jre,daemonRegistryDir=C:\Users\Ronald\.gradle\daemon,pid=7044,idleTimeout=null,daemonOpts=-Xmx1536m,-Dfile.encoding=windows-1252,-Duser.country=US,-Duser.language=en,-Duser.variant]
 Actual: DefaultDaemonContext[uid=7819e5ac-98ac-4dd2-8891-cacf4e56f662,javaHome=C:\Program Files\Java\jdk1.8.0_112,daemonRegistryDir=C:\Users\Ronald\.gradle\daemon,pid=4720,idleTimeout=60000,daemonOpts=-Xmx1536m,-Dfile.encoding=windows-1252,-Duser.country=US,-Duser.language=en,-Duser.variant]
 Consult IDE log for more details (Help | Show Log)

The solution would be to explicitly tell grade where your java directory is. You can do this in two places

  1. Open up %USERPROFILE%\.gradle\  and create the file gradle.properties
  2. In your android project create or edit the gradle.properties file

Inside that file put the location of your java path using forwardslashes instead of backslashes

org.gradle.java.home=Your/path/here/with/forwardslashes

In my case it looks like:

org.gradle.java.home=C:/Program Files/Java/jdk1.8.0_112
Virtualization: Should I assign CPU’s or Cores 0

Virtualization: Should I assign CPU’s or Cores

TL:DR Cores Cores Cores (but no more than pCPU count)

When creating a new virtual machine in VMware ESXi or VMware Workstation, it asks you how may processor cores you wish to set and gives you the option to set both ‘Number of processors‘ and ‘Number of cores per processor‘.

Most places online (looking at you Stack Overflow) said that only ‘Total processor cores‘ matters. I was curious as to how true that was and decided to run some tests myself.

The results are as follows

CPU’s Cores Single-Core Score Multi-Core Score % Increase
1 1  3159  2976  base
1 2  3161  4758  ↑60%
1 4  3055  5068  ↑70%
2 1  2991  4820  ↑62%
4 1  2960  4931  ↑65%
2 2  2913  5060  ↑70%

The results here were pretty astonishing, as soon as we increase the CPU count we actually lost single-core performance and gained a slightly higher multi-core score.

This may be because of the way VMware handle cores vs CPU’s. It could also be because of how the guest operating system handles multiple processors. Regardless, seeing as single-thread performance is more important in most applications, I’d stick with adding cores and not processors for the time being.

How many cores to assign to vmware virtual machines 0

How many cores to assign to vmware virtual machines

TL:DR Even with Hyperthreading enabled, do not exceed the amount of PHYSICAL cores your machine has. 

The General rule for assigning CPU’s is to keep your virtual cpu count at or lower than your physical cpu count. (VMware blog) .

I was curious as to why this was, seeing as generally one can get anywhere between a 10-30% increase in performance with hyperthreading.

To see how the performance scaled with each additional core, I decided to test it out on my laptop (1CPU, 2 Cores, 4ht cores). I spun up a new CentOS 7 machine and ran geekbench4 on each core configuration. The results were as follows

CPU’s Cores Single-Core Score Multi-Core Score % Increase
1 1  3159  2976  n/a
1 2  3161  4758  ↑60%
1 3  3109  4851  ↑2%
1 4  3055  5068  ↑4%

From our table we can see that after we arrived at the physical CPU count, adding extra cores gave us very little in terms of a performance boost.

Best linux distro for daily use in 2017 1

Best linux distro for daily use in 2017

A new year has begun. Time to format my disks. New windows install and more importantly, new Linux install. With windows, the choice is easy. Windows 10. With Linux, it’s a bit more complicated….

Here were the candidates

Ubuntu 16.04 LTS
Here’s a little history about my relationship with Ubuntu. Ubuntu was my first distro and all was right with the world, I would never have left… that is.. until the fire nation att… until unity. It was a bit of a messy breakup, something about it just didn’t resonate with my soul. A year or two ago I tried it again but I remember seeing amazon search results integrated by default and was instantly disgusted. But time has passed, seasons have changed and I believe people learn from their mistakes..so I wanted to give it another shot. so I did… (more…)