在Node.js Html和静态文件css、js中。结果是;“正在等待本地主机”;加载
这里没有足够的信息来说明什么是失败的,但是这里有一些选项可以帮助您排除故障 检查“网络”选项卡以查看哪些文件无法加载 添加一些console.log语句以查看实际运行的代码。这将缩小失败的范围 我想指出,您的路由逻辑非常奇怪。您只需检查文件的扩展名,并为该类型的每个请求提供相同的文件。例如,如果我请求/wricle/any/path/to/some.js,它将始终使用material.js进行响应。我猜你将来会想加载更多的js文件 我建议使用类似的框架。它可以让您轻松地为静态文件目录提供服务在Node.js Html和静态文件css、js中。结果是;“正在等待本地主机”;加载,html,node.js,Html,Node.js,这里没有足够的信息来说明什么是失败的,但是这里有一些选项可以帮助您排除故障 检查“网络”选项卡以查看哪些文件无法加载 添加一些console.log语句以查看实际运行的代码。这将缩小失败的范围 我想指出,您的路由逻辑非常奇怪。您只需检查文件的扩展名,并为该类型的每个请求提供相同的文件。例如,如果我请求/wricle/any/path/to/some.js,它将始终使用material.js进行响应。我猜你将来会想加载更多的js文件 我建议使用类似的框架。它可以让您轻松地为静态文件目录提供服务
var http = require('http'),
fs = require('fs');
http.createServer(function (req, res) {
if(req.url.indexOf('.html') != -1){ //req.url has the pathname, check if it conatins '.html'
fs.readFile(__dirname + '/public/index.html', function (err, data) {
if (err) console.log(err);
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
res.end();
});
}
if(req.url.indexOf('.js') != -1){ //req.url has the pathname, check if it conatins '.js'
fs.readFile(__dirname + '/public/js/material.js', function (err, data) {
if (err) console.log(err);
res.writeHead(200, {'Content-Type': 'text/javascript'});
res.write(data);
res.end();
});
}
if(req.url.indexOf('.css') != -1){ //req.url has the pathname, check if it conatins '.css'
fs.readFile(__dirname + '/public/css/material.css', function (err, data) {
if (err) console.log(err);
res.writeHead(200, {'Content-Type': 'text/css'});
res.write(data);
res.end();
});
}
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
这将允许您以静态文件的形式公开加载任何文件
- /js/material.js->services public/js/material.js
- /css/material.css->services public/css/material.css
app.use(express.static('public'))