我引用了之前提出的问题,但无法解决。我安装了 Express 服务器并尝试通过它运行 Index.html 文件。 但我收到“无法获取/”作为响应。
这是我调用index.html的server.js
var express = require('express');
var app = express();
app.get('index.html', function (req, res) {
app.use("/", express.static(__dirname));
});
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
提前致谢!!
最佳答案
当您访问托管站点上的目录时,例如端口 8080 上 localhost 的根目录,使用 http://localhost:8080/
作为 URL,浏览器不会发送以下请求: 'index.html` 到服务器并仅使用服务器发回的任何内容。
这是一个快速静态中间件,它响应没有文件名的浏览器请求,将检查该文件夹是否存在并(默认情况下)返回该文件夹中包含的任何index.html。因此,您的路由代码行 app.get('index.html')
永远不会执行,并且浏览器会向您显示错误消息。
如果您想尝试一下,这里有一个迷你静态 express 服务器。
var express = require('express');
var app = express();
app.use(express.static('../public')); // path to your public directory
var server = app.listen(8080, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
关于javascript - Express Server 在尝试访问 index.html 时返回 Cannot Get,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33224309/