我正在尝试将数组响应另一个 js 文件并从那里访问它。
我试过了,res.render()
但它给我一个错误,我没有模块,我不想安装任何模块。
我也试过了res.json()
,但这只是在浏览器上显示了数组,我不能用它做太多。
导入和导出不起作用。
获取此数组的特定 urlfetch(/anime)
也不起作用,因为当我尝试启动我的服务器时,url("/") 它试图获取我尚未发送的数组。这是因为我正在发送链接到另一个 js 文件的 html 文件。
我希望我说清楚。
服务器.js
app.get("/", (req, res) => {
app.use(express.static("./animeCatcher"));
res.sendFile("index.html");
})
app.get("/anime", (req, res) => {
let animeNames = ["One Piece", "Fairy Tail", "Attack on Titan"];
res.json(animeNames);
});
索引.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src= "animeScrape.js"></script>
</head>
<body>
</body>
</html>
动漫刮刀.js
fetch('/anime')
.then(res => res.json())
.then(animeNames => console.log(animeNames));
感谢您的时间。