2017-10-14 147 views
1

我会将我的Web应用程序部署到Heroku。 这是我的项目的树:将Heroku 4/NodeJS部署到Heroku

├── client // Angular project is Here 
├── db 
├── express-admin 
├── index.js 
├── node_modules 
├── package.json 
├── package-lock.json 
├── port 
├── Procfile 
├── README.md 
├── routes 
└── test.html 

我已经做下列步骤操作:

1)建立角4应用程序使用ng build -prod --aot=false

2)点的NodeJS应用角度指数.html文件在/ dist文件夹中。

这里的index.js文件:

// Get dependencies 
const express = require('express'); 
const path = require('path'); 
const http = require('http'); 
const bodyParser = require('body-parser'); 
var cool = require('cool-ascii-faces'); 

//var db = require('./db/connect.js'); 
// Get our API routes 
//const api = require('./server/routes/api'); 
var appRoutes = require('./routes/index'); 
const app = express(); 

// Parsers for POST data 
app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({ 
    extended: false 
})); 

// Point static path to dist 
app.use(express.static(path.join(__dirname, 'client/dist/'))); 
app.use(function(req, res, next) { 
    res.header("Access-Control-Allow-Origin", "*"); 
    res.header('Access-Control-Allow-Methods', 'GET, POST, PUT ,DELETE'); 

    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); 
    next(); 
}); 
app.use('/', appRoutes) 
// Set our api routes 
//app.use('/api', api); 

// Catch all other routes and return the index file 
app.get('/', (req, res) => { 
    res.sendFile(path.join(__dirname, 'client/dist/index.html')); 
}); 
app.get('/cool', function(request, response) { 
    response.send(cool()); 
}); 
/** 
* Get port from environment and store in Express. 
*/ 
const port = process.env.PORT || '4001'; 
app.set('port', port); 

/** 
* Create HTTP server. 
*/ 
const server = http.createServer(app); 

/** 
* Listen on provided port, on all network interfaces. 
*/ 
server.listen(port,() => console.log(`API running on localhost:${port}`)); 

3)我用official Heroku Guide部署应用程序的NodeJS

当我打开由Heroku上生成的链接,索引页获得加载但当我尝试使用登录我得到这个错误:

Mixed Content: The page at 'https://lit-island-95274.herokuapp.com/signup' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'http://localhost:4001/login'. This request has been blocked; the content must be served over HTTPS. 

这是我的角度的package.json文件:

{ 
    "name": "projecttt", 
    "version": "0.0.0", 
    "license": "MIT", 
    "scripts": { 
    "ng": "ng", 
    "start": "ng serve", 
    "build": "ng build", 
    "test": "ng test", 
    "lint": "ng lint", 
    "e2e": "ng e2e" 
    }, 
    "private": true, 
    "dependencies": { 
    "@angular/animations": "^4.1.2", 
    "@angular/common": "^4.1.2", 
    "@angular/compiler": "^4.1.2", 
    "@angular/core": "^4.1.2", 
    "@angular/flex-layout": "^2.0.0-beta.7", 
    "@angular/forms": "^4.1.2", 
    "@angular/http": "^4.1.2", 
    "@angular/material": "2.0.0-beta.4", 
    "@angular/platform-browser": "^4.1.2", 
    "@angular/platform-browser-dynamic": "^4.1.2", 
    "@angular/router": "^4.1.2", 
    "angular-2-local-storage": "^1.0.1", 
    "hammerjs": "^2.0.8", 
    "core-js": "^2.4.1", 
    "rxjs": "^5.4.1", 
    "systemjs": "^0.20.14", 
    "typings": "^2.1.1", 
    "zone.js": "^0.8.4" 
    }, 
    "devDependencies": { 
    "@angular/cli": "^1.4.7", 
    "@angular/compiler-cli": "^4.4.0", 
    "@angular/language-service": "^4.0.0", 
    "@types/jasmine": "~2.5.53", 
    "@types/jasminewd2": "~2.0.2", 
    "@types/node": "^8.0.7", 
    "codelyzer": "~3.0.1", 
    "jasmine-core": "~2.6.2", 
    "jasmine-spec-reporter": "~4.1.0", 
    "karma": "~1.7.0", 
    "karma-chrome-launcher": "~2.1.1", 
    "karma-cli": "~1.0.1", 
    "karma-coverage-istanbul-reporter": "^1.2.1", 
    "karma-jasmine": "~1.1.0", 
    "karma-jasmine-html-reporter": "^0.2.2", 
    "lite-server": "^2.2.2", 
    "lodash": "^4.16.4", 
    "protractor": "~5.1.2", 
    "rimraf": "^2.5.4", 
    "ts-node": "~3.0.4", 
    "tslint": "~5.3.2", 
    "typescript": "^2.3.4" 
    }, 
    "repository": {}, 
    "main": "karma-test-shim.js" 
} 

如果我有很好的理解,角HTTP路线没有得到部署,因为Heroku上没有建筑角的应用程序,但怎么办呢?

+0

在构建的Angular应用程序中,您看起来有错误的API端点。在您使用Angular CLI时,请查看环境功能:https://github.com/angular/angular-cli/wiki/stories-application-environments。如果你像这样部署,生产环节应该是相对的。 – jonrsharpe

+0

我不明白如何解决我的问题与您的链接 – infodev

回答

0

默认的Heroku不安装任何因此开发依赖移动你的

"@angular/cli": "^1.4.7", 
"@angular/compiler-cli": "^4.4.0", 
"typescript": "^2.3.4" 

进行依赖部分,然后你可以部署。 由于这些依赖项在devDependency中,所以heroku无法找到导致部署不成功的库。 希望能帮到你。

+0

和angularJS项目是什么? –