提问者:小点点

尝试获取Node.js项目以连接到javascript项目


我正试图让node.js将信息发送回一个网站我正试图从这里获取信息(rn它是一个临时值),但是我如何使用JavaScript获取信息呢?或者我需要更改node.js项目中的某些内容吗

const app = require('express')();

app.get('/', (req, res) => 
{
  console.log("tried")
res.send({"body":"important info"});
//the info passed on can't be a number
}
);
  app.listen(3000);

当我试着

fetch("https://discbottest-2.mathman05.repl.co")
.then(r => r.json())
.then(console.log)
.catch(console.error);

它将错误:“TypeError:Failed to Fetch”


共1个答案

匿名用户

你可能和科斯有矛盾。在express中,很容易禁用cors npm模块进行开发。在使用npm install cors安装它之后,类似这样的内容应该可以帮助您解决问题

null

const app = require('express')();
app.use(require("cors")());
app.get('/', (req, res) => {
    console.log("tried")
    res.send({ "body": "important info" });
    //the info passed on can't be a number
});
app.listen(3000);