【发布时间】:2021-03-14 19:17:39
【问题描述】:
我正在尝试部署一个普通的应用程序,但我不知道如何部署。部署后,我的 API 没有连接到我的前端,但我可以在端口:3000 上本地运行应用程序,但是当我尝试在端口:8080 上运行它以尝试部署到 heroku 时,它不会运行。我试图部署到firebase,但它只是弹出前端,什么都不做。 这是我的 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
这里是。我对我的 api 的每个端点的调用。这是我的约会.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}`);
}
}
这是我在 enviorment.ts/enviorment.prod.ts 文件中对 API_URL 的定义,我不知道该放什么。
export const environment = {
production: true,
API_URL: 'http://localhost:3000',
};
【问题讨论】:
-
您是否在浏览器中检查了网络检查?需要检查您的网站实际到达的端点
-
@development-ninja 你是什么意思,比如在部署到firebase或本地时检查它?
-
@development-ninja 这就是它告诉我的 POST localhost:3000/appointments net::ERR_CONNECTION_REFUSED
-
好的,这意味着找不到 api,因为 url 路径在部署到实时服务器时不应包含 localhost。您应该处理路径以确保 api 应该工作。
-
@development-ninja 我应该删除 API_URL 变量吗?或留空
标签: angular mongodb firebase api mean