aMighty
aMighty

Reputation: 283

How to host AngularJS with Node on Heroku

I am following Heroku's documentation for hosting application on Heroku and I have successfully hosted my nodejs API on Heroku but I'm not able to host my angularJs code as it doesn't have Server.js file in my project.

Upvotes: 0

Views: 323

Answers (2)

Fatih Turgut
Fatih Turgut

Reputation: 349

You should have "server.js" and "Procfile" files.

https://devcenter.heroku.com/articles/procfile

Push those to heroku:

-angular-project folder

-Procfile

web: node server.js

-server.js

'use strict'

var express = require('express');

// App
var app = express();
app.set('port', (process.env.PORT || 5000));

// your angular-project folder
app.use('/', express.static(__dirname + '/angular-project'));

app.listen(app.get('port'), function() {
  console.log("running: port", app.get('port'));
});

Upvotes: 2

Rotem jackoby
Rotem jackoby

Reputation: 22058

The best option is to serve angular via CDN or NgInx.

If you want to serve it with nodeJS just add a server.js (or index.js) file with the following code:

var express     = require('express'),
    feapp       = express(),
    path        = require('path'),
    bodyParser  = require('body-parser'),
    fs          = require('fs'),
    compression = require('compression');


//Setup
feapp.use(compression({ threshold: 0 }));
feapp.use(express.static(path.join(__dirname, '..' ,'/public') , { maxAge: 3600 } )); //Use Cache-Control for performance
feapp.use(bodyParser.json());////For parsing application/json
feapp.use(bodyParser.urlencoded({extended: true}));
feapp.use(express.static(__dirname));


//HTTP server
http    = require('http'); //Or https - but you will have add SSL certificats
var FE_HTTP_PORT = 80;

var feServer = http.createServer(feapp);
feServer.listen(FE_HTTP_PORT, function() {
    console.log('Listening on port ', FE_HTTP_PORT);
});


// Simple Routing
feapp.get('*', function(req, res){
     res.sendFile(path.join(__dirname, '..' ,'/public', 'index_site.html'));
});


module.exports = feapp;

Upvotes: 1

Related Questions