gpt4 book ai didi

javascript - 将服务器端存储的 javascript 文件添加到 html (node.js)

转载 作者:行者123 更新时间:2023-11-28 20:02:57 25 4
gpt4 key购买 nike

我的项目遇到了一个小问题。我正在使用 Node 和 Express 来创建一个小网页..它正在渲染 html 页面,但 javascript 文件 (test.js) 不会发送给用户..

<!doctype html>
<html>
<head>
<title>Spektrum</title>
<script src="test.js"></script>
<meta name = "viewport" content = "initial-scale = 1, user-scalable = no">
<style>
canvas{

}
</style>
</head>
<body>
<!-- some stuff -->
</body>
</html>

Node.js 文件:

var http = require('http'),  
fs = require('fs'),
express = require('express'),
app = express();
app.use(express.bodyParser());
app.set('view engine', 'ejs');
app.engine('html', require('ejs').renderFile);

// .....
app.listen(8080, function() {
console.log('Server running at http://127.0.0.1:8080/');
});

知道如何将带有 html 文件的 javascript 文件发送给用户吗?

您好,JS

最佳答案

您需要使用static中间件来提供文件(test.js需要存在)

示例:

// GET /javascripts/jquery.js
// GET /style.css
// GET /favicon.ico
app.use(express.static(__dirname + '/public'));

关于javascript - 将服务器端存储的 javascript 文件添加到 html (node.js),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21312259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com