You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NodeJS/Express部署cPanel后出现503错误及CORS问题求助

Angular+Express部署cPanel出现503+CORS问题的排查与解决

问题场景

Angular前端搭配Node.js/Express后端的站点,本地运行完全正常,但部署到cPanel的NodeJS环境后,始终返回503错误,最初怀疑是CORS配置问题。

排查过程

  • 使用POSTMAN测试后端接口,同样返回503错误
  • 参考资料尝试添加app.use(cors()),问题未得到解决
  • 前后端部署在同一服务器:前端域名www.server.com,后端域名be.server.com,均采用HTTPS协议
  • 后端服务监听端口4040,A2 Hosting客服提示生产环境API调用不要携带端口
  • 在cPanel终端手动启动后端启动文件无报错,但调用接口时返回404
  • 直接访问后端域名be.server.com也出现503错误,没有预期的cannot GET /提示

相关代码片段

后端CORS处理代码(app.js)

const express = require('express');
const bodyParser = require('body-parser');
const app = express();

// added this bit after reading other threads on SO
app.options("*", (req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,POST,OPTIONS');
    res.header('Access-Control-Allow-Headers', 'Authorization, Content-Type');        
    res.sendStatus(200).end();
});

app.use((req, res, next) => {
    console.log('Request received:', req.method, req.url);
    console.log('Headers:', req.headers);
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,POST,OPTIONS');
    res.header('Access-Control-Allow-Headers', 'Authorization, Content-Type');
    next();
});

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

const userRoutes = require('./routes/user');

// all the various api calls...
app.use("/api/user", userRoutes);

module.exports = app;

前端API调用示例

this.http.post<DatabaseReturn>(`https://be.server.com/api/user/login`, { email, password }).pipe(take(1));

前端请求头设置

// set an Authorization header with the value "Bearer <their unique token OR undefined>"
headers: req.headers.set('Authorization', "Bearer " + token)

浏览器控制台错误信息

XHROPTIONS https://be.server.com/api/user/login CORS Missing Allow Origin
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://be.server.com/api/user/login. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing). Status code: 503.
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://be.server.com/api/user/login. (Reason: CORS request did not succeed). Status code: (null).
Error: [object Object]

最终解决方案

问题根源出在forever-monitor组件上,移除该组件后直接运行后端服务器,503错误和CORS相关提示立即消失,站点恢复正常运行。

内容的提问来源于stack exchange,提问作者Bogomip

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 21:36:32