[英]Deploying an angular node app, with aws ec2 - Error: Could not connect to the server
我正在嘗試在 aws ec2 中部署一個角度/節點應用程序,但我遇到了這個錯誤,我無法解決。
我的應用程序是使用ng build --prod
我有這個文件結構
我這樣調用節點:
...
const AUTH_API = 'http://localhost:8080/api/authPsi/';
...
login(credentials): Observable<any> {
return this.http.post(AUTH_API + 'signin', {
email: credentials.email,
password: credentials.password
}, httpOptions);
}
...
我也嘗試過使用服務器 IP 。
在 server.js 我有這個 cors 政策
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const dbConfig = require("./db.config");
const app = express();
var corsOptions = {
origin: "http://localhost:4200"
};
app.use(cors(corsOptions));
// parse requests of content-type - application/json
app.use(bodyParser.json());
我的 /etc/nginx/sites-available/default
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name localhost;
root /dist/psiquests;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
我相信我已經把我需要解釋的一切都說了,但是如果您需要更多信息,請告訴我。
我相信這是一個 cors 問題,但不知道如何解決。
謝謝!
在我看來,您在瀏覽器中調用了 localhost url。 發生這種情況時,您的計算機會將請求解析到它自己的本地主機。 如果要將其部署到服務器,則需要使用與訪問頁面相同的 url 服務。
例如,如果您的頁面是從www.domain.com加載的,那么您的AUTH_API
變量看起來應該是www.domain.com/your_auth_path 。
編輯:我看到您說您也嘗試過使用服務器 IP 。 如果您發布所有示例會更有幫助。 如果不是本地主機分辨率,則可能是 COR 配置不正確。
如果您的后端和前端都來自同一個域和子域,則您不需要啟用 COR。
排除是否是 COR 錯誤的一個好方法是重試 Postman 中的請求。 如果請求在那里成功,那么 99% 是 COR 的問題。
使用以下方法解決了:
...
const AUTH_API = '/api/authPsi/';
...
login(credentials): Observable<any> {
return this.http.post(AUTH_API + 'signin', {
email: credentials.email,
password: credentials.password
}, httpOptions);
}
...
和這個 nginx 的配置文件
server {
charset utf-8;
listen 80 default_server;
server_name _;
location / {
root dist/folder;
try_files $uri /index.html;
}
location /api/{
proxy_pass http://localhost:8080;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.