最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

如何在此节点服务器上启用cors

IT培训 admin 13浏览 0评论

如何在此节点服务器上启用cors

学习节点,我正在寻找有关将服务器的CORS声明放在何处的建议。我有一个在://example:4343上运行的节点服务器。

目标是使用CSS和节点应用程序上站点的字体。对于CSS,Cors没有问题。对于字体,CORS禁止下载

实际代码如下:

const https = require("https"),
  fs = require("fs");

const options = {
  key: fs.readFileSync("/etc/letsencrypt/live/***/privkey.pem"),
  cert: fs.readFileSync("/etc/letsencrypt/live/***/fullchain.pem")
};

const express = require("express");
const app = express();
const { resolve } = require("path");
// Copy the .env.example in the root into a .env file in this folder

const env = require("dotenv").config({ path: "./.env" });
const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);

app.use(express.static(process.env.STATIC_DIR));
app.use(
  express.json({
    // We need the raw body to verify webhook signatures.
    // Let's compute it only when hitting the Stripe webhook endpoint.
    verify: function(req, res, buf) {
      if (req.originalUrl.startsWith("/webhook")) {
        req.rawBody = buf.toString();
      }
    }
  })
);

我知道我必须放这样的东西:

res.header("Access-Control-Allow-Origin", "");

启用在example上运行的节点服务器中的域中的字体:4343但是在哪里以及如何放置呢?还是我需要其他东西?

我请求的实际标题是:

Host: example
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:75.0) Gecko/20100101 Firefox/75.0
Accept: application/font-woff2;q=1.0,application/font-woff;q=0.9,*/*;q=0.8
Accept-Language: it-IT,it;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: identity
Origin: :4343
Connection: keep-alive
Referer: .min.css
If-Modified-Since: Thu, 30 Apr 2020 14:19:09 GMT
If-None-Match: "466c-5a482bf8bff8e"
Cache-Control: max-age=0
回答如下:

据我了解,您已经在example网站上保存了字体,并且您想在基于Node的example:4343网站上使用它们。

允许CORS取决于为https://example网站提供服务的软件。

您不应在运行example:4343的Node应用程序中允许CORS,而应在提供example的Web服务器中允许CORS。

如果Web服务器是Apache,则为add this line in the .htaccess file。

.htaccess是使用NGINX时的设置方式。

如果exam​​ple Web服务器正在使用Node,我建议您使用This中间件来表达:

  • cors

    npm install cors

const express = require('express') const cors = require('cors') const app = express() app.use(cors()) 中所述

如何在此节点服务器上启用cors

学习节点,我正在寻找有关将服务器的CORS声明放在何处的建议。我有一个在://example:4343上运行的节点服务器。

目标是使用CSS和节点应用程序上站点的字体。对于CSS,Cors没有问题。对于字体,CORS禁止下载

实际代码如下:

const https = require("https"),
  fs = require("fs");

const options = {
  key: fs.readFileSync("/etc/letsencrypt/live/***/privkey.pem"),
  cert: fs.readFileSync("/etc/letsencrypt/live/***/fullchain.pem")
};

const express = require("express");
const app = express();
const { resolve } = require("path");
// Copy the .env.example in the root into a .env file in this folder

const env = require("dotenv").config({ path: "./.env" });
const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);

app.use(express.static(process.env.STATIC_DIR));
app.use(
  express.json({
    // We need the raw body to verify webhook signatures.
    // Let's compute it only when hitting the Stripe webhook endpoint.
    verify: function(req, res, buf) {
      if (req.originalUrl.startsWith("/webhook")) {
        req.rawBody = buf.toString();
      }
    }
  })
);

我知道我必须放这样的东西:

res.header("Access-Control-Allow-Origin", "");

启用在example上运行的节点服务器中的域中的字体:4343但是在哪里以及如何放置呢?还是我需要其他东西?

我请求的实际标题是:

Host: example
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:75.0) Gecko/20100101 Firefox/75.0
Accept: application/font-woff2;q=1.0,application/font-woff;q=0.9,*/*;q=0.8
Accept-Language: it-IT,it;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: identity
Origin: :4343
Connection: keep-alive
Referer: .min.css
If-Modified-Since: Thu, 30 Apr 2020 14:19:09 GMT
If-None-Match: "466c-5a482bf8bff8e"
Cache-Control: max-age=0
回答如下:

据我了解,您已经在example网站上保存了字体,并且您想在基于Node的example:4343网站上使用它们。

允许CORS取决于为https://example网站提供服务的软件。

您不应在运行example:4343的Node应用程序中允许CORS,而应在提供example的Web服务器中允许CORS。

如果Web服务器是Apache,则为add this line in the .htaccess file。

.htaccess是使用NGINX时的设置方式。

如果exam​​ple Web服务器正在使用Node,我建议您使用This中间件来表达:

  • cors

    npm install cors

const express = require('express') const cors = require('cors') const app = express() app.use(cors()) 中所述

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论