Ubuntu 如何在systemD服务中配置CORS?

Ubuntu 如何在systemD服务中配置CORS?,ubuntu,cors,systemd,Ubuntu,Cors,Systemd,在systemD服务中设置CORS无效 我有一个启用CORS的节点模块,有没有办法将CORS配置从代码移动到systemd服务 代码中的当前设置: private allowCrossDomain(req: express.Request, res: express.Response, next: () => void) { res.header('Access-Control-Allow-Origin', 'http://localhost:4200');

在systemD服务中设置CORS无效

我有一个启用CORS的节点模块,有没有办法将CORS配置从代码移动到systemd服务

代码中的当前设置:

private allowCrossDomain(req: express.Request, res: express.Response, next: () => void) {
        res.header('Access-Control-Allow-Origin', 'http://localhost:4200');
        res.header('Access-Control-Allow-Headers', 'Authorization, Content-Type');
        res.header('Access-Control-Allow-Methods', 'GET,PUT,POST');
        res.header('Access-Control-Allow-Credentials', 'true');
        next();
    }
希望在myService.service中添加CORS,但以下设置不起作用:

[unit]
Description=Tool operation BE
After=network.target
[Service]
User=ubuntu
Environment=BACKEND_HOST=backend-ops-model.com
Environment=BACKEN_MODEL_PORT=80
Environment=res.header='Access-Control-Allow-Origin','http://example.com'
WorkingDirectory=/opt/backend-service/operation
ExecStart=/usr/bin/node --experimental-worker /opt/backend-service/operation/node_modules/@gst/operation-service/www.js
TimeoutStopSec=10
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target


这就是解决问题的方法。在代码中添加CORS选项作为环境变量:

  private allowCrossDomain(req: express.Request, res: express.Response, next: () => void) {
        res.header('Access-Control-Allow-Origin', process.env.CORS_ORIGIN_HOST || 'http://localhost:4200');
        ...
然后在systemd配置中添加环境指令:

[unit]
Description=Tool operation BE
After=network.target
[Service]
User=ubuntu
Environment=BACKEND_HOST=backend-ops-model.com
Environment=BACKEN_MODEL_PORT=80
Environment=CORS_ORIGIN_HOST=http://alterntive.com
WorkingDirectory=/opt/backend-service/operation
ExecStart=/usr/bin/node --experimental-worker /opt/backend-service/operation/node_modules/@gst/operation-service/www.js
TimeoutStopSec=10
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target
在上面的配置中,如果您没有在systemd中提供环境变量,则会选择localhost:4200作为默认值。
希望它能帮助其他人解决同样的问题。

我投票结束这个问题,因为关于Linux操作系统及其实用程序的问题应该被问到,因为这个问题已经被问到并回答了