[英]I can run MEAN app locally, but can't deploy it
I am trying to deploy a mean app, but I cannot figure out how.我正在尝试部署一个平均应用程序,但我不知道如何。 My API isn't connecting to my frontend once I deploy, but I can run the application locally on port:3000, but when I try to run it on port:8080 to try and deploy to heroku it doesn't run.
我的 API 部署后没有连接到我的前端,但是我可以在端口:3000 上本地运行应用程序,但是当我尝试在端口:8080 上运行它以尝试部署到 heroku 时,它没有运行。 I have tried to deploy to firebase, but it just pops up the front-end, and does nothing.
我曾尝试部署到 firebase,但它只是弹出前端,什么都不做。 Here is my WWWW/Bin file:
这是我的 WWWW/Bin 文件:
enter code here
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('api:server');
var http = require('http');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
var server = http.createServer(app);
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
};
onListening()
console.log("Node server running on port : 3000" );
enter code here
Here is.这是。 my calls to each endpoint of my api.
我对 api 的每个端点的调用。 This is my appointment.services.ts file
这是我的约会.services.ts 文件
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { Appointment } from './Appointment';
import { environment } from 'src/environments/environment';
@Injectable({
providedIn: 'root'
})
export class AppointmentService {
private BASE_URL = environment.API_URL;
constructor(private http: HttpClient) { }
getAppointments(): Observable<Appointment[]> {
return this.http.get<Appointment[]>(`${this.BASE_URL}/appointments`);
}
createAppointment(appointmentDate: string, appointmentTime: string, name: string, email: string, phone: string): Observable<Appointment> {
return this.http.post<Appointment>(`${this.BASE_URL}/appointments`, { appointmentDate,appointmentTime, name, email,phone});
}
cancelAppointment(id: string): Observable<any> {
return this.http.delete(`${this.BASE_URL}/appointments/${id}`);
}
}
Here is my definition of my API_URL inside my enviorment.ts/enviorment.prod.ts files, im not sure what to put inside of this.这是我在 enviorment.ts/enviorment.prod.ts 文件中对 API_URL 的定义,我不确定在其中放入什么。
export const environment = {
production: true,
API_URL: 'http://localhost:3000',
};
First you should make your local api to be hosted on https:// by hosting your app on firebase cloud function.首先,您应该将您的本地 api 托管在 https:// 上,方法是将您的应用程序托管在 firebase 云 ZC1C4250268E679A 上。 Once you make it then please test it returns same result in same response type as your localhost:3000/appointments.
完成后,请测试它以与 localhost:3000/appointments 相同的响应类型返回相同的结果。 Firebase cloudfunction is serverless function which communicates via https protocol.
Firebase 云函数是无服务器的 function,它通过 https 协议进行通信。 The exported url will be
https://us-central1-..
导出的 url 将为
https://us-central1-..
And update your angular frontend to change base_url to have above api without /appointments
to ensure it can be accessed via https live hosting.并更新您的 angular 前端以将 base_url 更改为具有以上 api 而无需
/appointments
以确保可以通过 https 实时托管访问它。 Make this cloudfunction on your admin在您的管理员上创建此云功能
// cloudfunction
exports.appointments= functions.https.onRequest((req, res) => {
return cors(req, res, () => { //its good for you to install cors package on your cloud function side to avoid any cors error. npm install cors
//your code here
return res.status(200).send(result)
});
});
// your angular frontend
export class AppointmentService {
//your code
private BASE_URL = "https://us-central1-.." //your cloudfunction url without /appointments. this is root url of all your apis
Then build and deploy again.然后再次构建和部署。 Atleast we should see the hosted live site access to https endpoint which were made by cloudfunction
至少我们应该看到由cloudfunction制作的https端点的托管现场访问
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.