当前位置: 首页 > 知识库问答 >
问题:

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

柴宏阔
2023-03-14

我正试图让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个答案

梅欣然
2023-03-14

你可能和科斯有矛盾。在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);
 类似资料:
  • 这是一个非常基本的问题,我有Centos 7,我配置了JAVA_HOME和Maven: [nodet@localhostWildFly]$mvn-版本Apache Maven 3.3.9(bb52d8502b132ec0a5a3f4c09453c07478323dc5;2015-11-10T17:41:47 01:00)Maven home:/data/Maven Java版本:1.8.0_101

  • 我不擅长java,还在尝试如何通过jdbc驱动程序将java连接到mysql(xampp),有人知道我为什么总是出错吗? 以下是控制台结果 我已经创建了数据库并插入了两个数据,其中一个是“username=rod1,password=rod”

  • 我目前正在开发一个应用程序,可以在用户手机上找到所有MP3,然后将它们放入列表中。这是非常好的工作,非常快,即使有很多歌曲。现在,我为列表中的每个项目填充一个新列表,然后将其显示在我的recyclerview中。问题是,我的手机上有700首歌曲,这在相当长的一段时间内会阻塞UI线程。 现在,我想使用回收站视图不将列表中的所有项目一次加载到对象中,而是仅在它们即将显示时加载-但我不知道如何做到这一点

  • 尝试连接到SQL服务器2014时出错。我使用JRE7和sqljdbc4-4.0.jar 以下是我的java代码: 以下是完整的堆栈跟踪: JAVAext.dirs:C:\Cisco\CallStudio\eclipse\jre\lib\ext;C:\WINDOWS\Sun\Java\lib\ext-com。微软sqlserver。jdbc。SQLServerException:驱动程序无法使用安全

  • 以springboot项目为例,完整项目可参考sop-example下的sop-story pom.xml添加版本配置 <!-- springboot 版本--> <spring-boot.version>2.3.2.RELEASE</spring-boot.version> <!-- spring cloud 版本 --> <spring-cloud.version>Hoxton.SR8</sp

  • 我试图使用NetBeans8.2用tomcat部署一个web项目。