CORS 中间件在 nodeJS 中不起作用
CORS middleware doesn't work in nodeJS
我在通过 localtunnel 托管到 public 的 raspberry pi 服务器上休息 api,并尝试从本地主机节点应用程序访问 API。本地节点应用程序是 运行 on express 并且有一些静态 JS 文件。从其中一个静态 JS 文件,我正在执行 axios ajax 请求休息 api 但将 CORS 错误发送到控制台:
script.js
setTimeout(() => {
axios.get('https://wvoegmuism.localtunnel.me/api/ninjas')
.then(function (question) {
var data = question.data;
const questions = [];
$.each(data, function(index, value) {
console.log(JSON.stringify(value.the_question, null, "\t"))
console.log(JSON.stringify(value.the_answer, null, "\t"))
console.log(JSON.stringify(value.the_choices, null, "\t"))
questions.push(new Question(value.the_question, value.the_choices, value.the_answer))
//console.log(data[index].the_question);
})
quiz = new Quiz(questions)
populate()
}), 1000});
我将 CORS 模块包含到我的 express 文件中 app.js:
var cors = require("cors");
app.use(cors());
我的休息 api 将另一台服务器上的文件路由到端点:
var express = require("express");
var app = express();
var router = express.Router();
var mongoose = require("mongoose");
var customerschema = require("../models/customersSchema");
var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
var config = require('../config'); // get our config file
var user = require("../models/user");
var bodyParser = require("body-parser");
app.use(bodyParser.json());
app.set('superSecret', "tokenproject"); // secret variable
mongoose.Promise = require('bluebird');
router.get('/ninjas', function(req, res) {
customerschema.find({}).then(function(ninjas) {
res.send(ninjas);
})
});
控制台错误:
Failed to load https://wvoegmuism.localtunnel.me/api/ninjas: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.
按照教程进行操作时,它适用于一个人,但不适用于我。我在这里缺少什么?我是否应该将 CORS 中间件不仅包含到我的本地节点应用程序中,还应该包含到 REST API 路由中?
Should I include CORS middleware not only to my local node app but
also to REST API routes?
没错! Api-端点需要打开电源线。
我在通过 localtunnel 托管到 public 的 raspberry pi 服务器上休息 api,并尝试从本地主机节点应用程序访问 API。本地节点应用程序是 运行 on express 并且有一些静态 JS 文件。从其中一个静态 JS 文件,我正在执行 axios ajax 请求休息 api 但将 CORS 错误发送到控制台:
script.js
setTimeout(() => {
axios.get('https://wvoegmuism.localtunnel.me/api/ninjas')
.then(function (question) {
var data = question.data;
const questions = [];
$.each(data, function(index, value) {
console.log(JSON.stringify(value.the_question, null, "\t"))
console.log(JSON.stringify(value.the_answer, null, "\t"))
console.log(JSON.stringify(value.the_choices, null, "\t"))
questions.push(new Question(value.the_question, value.the_choices, value.the_answer))
//console.log(data[index].the_question);
})
quiz = new Quiz(questions)
populate()
}), 1000});
我将 CORS 模块包含到我的 express 文件中 app.js:
var cors = require("cors");
app.use(cors());
我的休息 api 将另一台服务器上的文件路由到端点:
var express = require("express");
var app = express();
var router = express.Router();
var mongoose = require("mongoose");
var customerschema = require("../models/customersSchema");
var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
var config = require('../config'); // get our config file
var user = require("../models/user");
var bodyParser = require("body-parser");
app.use(bodyParser.json());
app.set('superSecret', "tokenproject"); // secret variable
mongoose.Promise = require('bluebird');
router.get('/ninjas', function(req, res) {
customerschema.find({}).then(function(ninjas) {
res.send(ninjas);
})
});
控制台错误:
Failed to load https://wvoegmuism.localtunnel.me/api/ninjas: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.
按照教程进行操作时,它适用于一个人,但不适用于我。我在这里缺少什么?我是否应该将 CORS 中间件不仅包含到我的本地节点应用程序中,还应该包含到 REST API 路由中?
Should I include CORS middleware not only to my local node app but also to REST API routes?
没错! Api-端点需要打开电源线。