我使用 nodejs(在 docker-compose 容器中,不確定這是否相關)來設定本地 Web 應用程式。我想在客戶端使用 jquery 將 HTML 檔案加載到 div 中。不幸的是,它不起作用。初始 index.html 和 new.html 位于同一檔案夾 (/frontend/) 中。我錯過了什么?
Node.js 應用程式:
var app = express();
// home route returns rendered html content
app.get("/", (req, res) => {
res.sendFile(__dirname "/frontend/index.html");
});
app.listen(3000, function(){
console.log('Example app listening on port 3000!');
});
索引.html:
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"
integrity="sha256-/xUj 3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="
crossorigin="anonymous">
</script>
<script type="text/javascript">
$(document).ready(function(){
$("#imported").load("new.html");
});
</script>
</head>
<body>
<div> This is content of the index HTML and below is the imported HTML:</div>
<div id="imported"></div>
</body>
</html>
最后是我想匯入 new.html 的 HTML:
<HTML>
<head>
</head>
<body>
<p>This is demo text.<p>
</body>
</html>
uj5u.com熱心網友回復:
僅express.static()用于提供靜態 HTML 檔案
const app = express();
app.use(express.static("frontend"))
app.listen(3000, function(){
console.log('Example app listening on port 3000!');
});
如果您只是在尋找一個普通的舊靜態檔案服務器,您可能會發現服務更簡單......
npx serve frontend
uj5u.com熱心網友回復:
基本上不提供 new.html 檔案。
您需要使用以下代碼在名為public.
var app = express();
// Put new.html into public folder.
// /public/new.html
// /frontend/index.html
app.use(express.static('public'))
// home route returns rendered html content
app.get("/", (req, res) => {
res.sendFile(__dirname "/frontend/index.html");
});
app.listen(3000, function(){
console.log('Example app listening on port 3000!');
});
參考:https : //expressjs.com/en/starter/static-files.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/379595.html
標籤:javascript html 查询 节点.js 表达
上一篇:替換腳本標簽型別并應用它
