如何在Heroku上使用Node托管AngularJS

时间:2018-08-01 10:18:04

标签: angularjs node.js heroku

我正在遵循Heroku在Heroku上托管应用程序的文档,并且已经在Heroku上成功托管了我的nodejs API,但是由于在我的项目中没有Server.js文件,所以我无法托管我的angularJs代码。

2 个答案:

答案 0 :(得分:2)

您应该具有“ server.js”和“ Procfile”文件。

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

将其推送到heroku:

-angular-project 文件夹

-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'));
});

答案 1 :(得分:1)

最好的选择是通过CDN或NgInx提供角度服务。

如果要与nodeJS一起使用,只需添加带有以下代码的server.js(或index.js)文件:

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;