node.js不加载javascript文件



我是node.js的新手。

我的问题是,html文件'index.html'称为'localhost:5000'似乎不加载javascript文件'test.js'。

我用 启动服务器nodejs server.js

来自shell的。文件是

server.js:

var http = require('http');
const process = require('process'); 
var fs = require('fs');
var server = http.createServer(function (request, response) {
if (request.url === "/") {
fs.readFile("index.html", function (error, pgResp) {
if (error) {
response.writeHead(404);
response.write('Page is not found');
} else {
response.writeHead(200, { 'Content-Type': 'text/html' });
response.write(pgResp);
}
response.end();
});
} else {
response.writeHead(200, { 'Content-Type': 'text/html' });
response.write('<h1>Default Content</h1>');
response.end();
}});
server.listen(5000);
console.log('Server is listening on 5000');
console.log(process.cwd())

路径process.cwd()是server.js、index.html和test.js三个文件的路径

index . html:

<html>
<body>
<center>
<h2>Testing NodeJS Application </h2>
<p> This is my first web application using NodeJS </p>
</center>
<script  type="text/javascript"  src="test.js"></script>
<script>
alert('Even this is a Test!');
</script>
</body>
</html>

和. js:

alert('I am here!');

index.html中的警告显示了,test.js中的警告没有显示。

我使用的是ubuntu 18.04。

Thanks in advance

彼得

问题是你导入test.js的方式HTML文件中的脚本:

<script  type="text/javascript"  src="test.js"></script>

你的浏览器正在加载这个文件,但是src=中提供的路由不能被解析,因为"test.js"不是一个有效的URL或路由到你可以加载它的文件。

你应该首先定义你要从哪里暴露你的"test.js"文件,所以它可以从你的浏览器中获取,然后,在src=的值纠正为实际的URL。

例如,一个常见的做法是公开服务器代码中/public/文件夹中的公共文件,然后在URL匹配/public/**some-file.js模式时公开那里的文件。

在你的代码中,你可以这样做:
//...
var path = require('path');
var server = http.createServer(function (request, response) {
if (request.url.startsWith("/public/")) {
fs.readFile(path.join(__dirname, path.normalize(req.url)), function (err,data) {
if (err) {
res.writeHead(404);
res.end(JSON.stringify(err));            
return;
}
res.writeHead(200);
res.end(data);
});
return;
}
if (request.url === "/") {
// ...      
} else {
// ...
}});
//... rest of your code

你可以在这个指南中找到更多的细节:如何提供静态文件。

这是我最后使用的代码。我将javascript文件test.js放在名为public.

的文件夹中。
var path = require('path');
var http = require('http');
var static = require('node-static');
const process = require('process'); 
var fs = require('fs');

var server = http.createServer(function (request, response) {
if (request.url.startsWith("/public/")) {
fs.readFile(path.join(__dirname, path.normalize(request.url)), function (err,data) {
if (err) {
response.writeHead(404);
response.end(JSON.stringify(err));            
return;
}
response.writeHead(200);
response.end(data);
});
return;
}
if (request.url === "/") {
fs.readFile("index.html", function (error, pgResp) {
if (error) {
response.writeHead(404);
response.write('Page is not found');
} else {
response.writeHead(200, { 'Content-Type': 'text/html' });
response.write(pgResp);
}
response.end();
});
} else {
response.writeHead(200, { 'Content-Type': 'text/html' });
response.write('<h1>Default Content</h1>');
response.end();
}
});
server.listen(5000);
console.log('Server is listening on 5000');
console.log(process.cwd())

多谢! !

最新更新