0

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.

2 Answers 2

2

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'));
});
Sign up to request clarification or add additional context in comments.

Comments

1

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.