无法在 Nodejs 中使用 Express 显示 index.html
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19620239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't get index.html to show with Express in Nodejs
提问by Paul Samsotha
I'm trying to run my first express app, but can't seem to get my webpage to show. I have the following code:
我正在尝试运行我的第一个快速应用程序,但似乎无法显示我的网页。我有以下代码:
var fs = require("fs");
var config = JSON.parse(fs.readFileSync("files/config.json"));
var host = config.host;
var port = config.port;
var express = require("express");
var app = express();
app.use(app.router);
app.use(express.static(__dirname + "/public"));
app.get("/", function(request, response){
response.send("hello!");
});
app.listen(port, host);
console.log("Listening on port" + port);
Here is my directory tree
这是我的目录树
nodejs/
js/
javascript.js
public/
index.html
I know the server is running because I get my "Hello!"response in the browser when I run 127.0.0.01:1337
我知道服务器正在运行,因为我"Hello!"在运行时在浏览器中得到响应127.0.0.01:1337
But when I try and type the webpage 1227.0.0.1:1337/index.html, I get Cannot GET /index.htmldisplayed in the browser
但是当我尝试输入网页时1227.0.0.1:1337/index.html,我Cannot GET /index.html会在浏览器中显示
So I'm guessing it's something wrong with the namevalue in my getmethod, but can't figure out what it is and how to fix it.
所以我猜测name我的get方法中的值有问题,但无法弄清楚它是什么以及如何修复它。
回答by JohnnyHK
Your app will only route page requests that are set up at the time of your app.use(app.router)call. So reorder your app.usecalls to be one of the following:
您的应用程序只会路由在您app.use(app.router)呼叫时设置的页面请求。因此app.use,将您的呼叫重新排序为以下之一:
Express 3
快递3
app.use(express.static(__dirname + "/public"));
app.use(app.router);
__dirnameis the directory that the executing script resides in, so because that lives in the jsdirectory that's a peer to publicyour code would need to be:
__dirname是执行脚本所在的目录,因为它位于与您的代码js对等的目录中,因此public需要:
app.use(express.static(__dirname + "/../public"));
app.use(app.router);
Express 4
快递4
Express 4 removes the need to manually do app.use(app.router). With Express 4 you just need:
Express 4无需手动执行app.use(app.router)。使用 Express 4,您只需要:
app.use(express.static(__dirname + "/../public"));
app.use(express.static(__dirname + "/../public"));
回答by Ajmal Aamir
I had this issue .... after a lot of trouble I find that if you run two workspaces or project at t time then it will create this scenario. so you might open only a workspace at a time and not just file ... open the hole folder then run the specific file.make the following change in your VS code setting.
我遇到了这个问题......经过很多麻烦之后,我发现如果你在 t 时间运行两个工作区或项目,那么它会创建这个场景。所以你可能一次只打开一个工作区而不仅仅是文件...打开孔文件夹然后运行特定的文件。在你的 VS 代码设置中进行以下更改。
setup the settings
设置设置
For 64bit system.
对于 64 位系统。
{
"liveServer.settings.AdvanceCustomBrowserCmdLine: ": "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",
"liveServer.settings.NoBrowser": false
}
For 32bit system.
对于 32 位系统。
{
"liveServer.settings.AdvanceCustomBrowserCmdLine: ": "C:\Program Files\Google\Chrome\Application\chrome.exe",
"liveServer.settings.NoBrowser": false
}

