在我的React / express应用的部署版本中路由RESTful API时遇到问题。
我已经在使用apache2的Ubuntu 16.04服务器的子目录中将节点应用程序“ portfolio”设置为子域。为了避免支付其他数字海洋飞沫,已经进行了很多配置。我设法用https将我的static react应用程序放在正确的子域中。 我一直坚持使用宁静的API。
var / www / html / portfolio / server.js包含:
import express from 'express';
import router from './routes/api/index';
import bodyParser from 'body-parser';
const app = express();
const port = process.env.PORT || 5000;
app.listen(port, () => console.log(`LISTENING on port ${port}`));
// Use middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(router);
portfolio / routes / api / index.js具有一些测试路由。 Express.Router的路由设置如下:
router.get('/api/hello', (req, res) => {
res.send({ express: 'Express back-end, up and running!',
});
});
通过pm2日志记录,我可以看到该节点是控制台,它将一些数据库查询记录到控制台和其他console.log命令。还要检查正在监听哪些端口,我可以看到有一个监听5000。
无论如何,部署中的问题都是从Portfolio / client / build / index.html提供服务,当然,浏览器是从服务器配置文件定向到https://portfolio.domain.com的。
在localhost上,我可以请求路由localhost:5000 / api / hello。
但是,这不起作用:https://portfolio.domain.com:5000/api/hello我猜是因为,当api可用时,这会请求-Portfolio / client / build / api / hello:5000 / portfolio / api / hello
编辑:更多信息!!!
现在,我发现需要配置apache。对于000-default.conf文件,我有一个空白的v / host:
<VirtualHost *:*>
ProxyPreserveHost On
# Servers to proxy the connection, or;
# List of application servers:
# Usage:
# ProxyPass / http://[IP Addr.]:[port]/
# ProxyPassReverse / http://[IP Addr.]:[port]/
# Example:
ProxyPass / http://0.0.0.0:8080/
ProxyPassReverse / http://0.0.0.0:8080/
ServerName localhost
</VirtualHost>
我也在domain.com和Portfolio.domain.com上都启用了SSL
Listen 443
NameVirtualHost *:443
<VirtualHost *:443>
SSLEngine On
# Set the path to SSL certificate
# Usage: SSLCertificateFile /path/to/cert.pem
SSLCertificateFile /etc/apache2/ssl/file.pem
# Servers to proxy the connection, or;
# List of application servers:
# Usage:
# ProxyPass / http://[IP Addr.]:[port]/
# ProxyPassReverse / http://[IP Addr.]:[port]/
# Example:
ProxyPass / http://0.0.0.0:8080/
ProxyPassReverse / http://0.0.0.0:8080/
# Or, balance the load:
# ProxyPass / balancer://balancer_cluster_name
</VirtualHost>
有了生产版本,子域,SSL和许多活动部件的目录结构,我不确定如何配置它。