带有 Express 错误的 NodeJS:无法获取 /

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13339695/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:37:45  来源:igfitidea点击:

NodeJS w/Express Error: Cannot GET /

node.jsgetexpress

提问by sia

This is what i have, the filename "default.htm" actually exists and loads when doing a readFile with NodeJS.

这就是我所拥有的,文件名“default.htm”实际上存在并在使用 NodeJS 执行 readFile 时加载。

var express = require('express');
var app = express();

app.use(express.static(__dirname + '/default.htm'));

app.listen(process.env.PORT);

The Error (in browser):

错误(在浏览器中):

Cannot GET /

回答by Sdedelbrock

You typically want to render templates like this:

您通常希望呈现这样的模板:

app.get('/', function(req, res){
  res.render('index.ejs');
});

However you can also deliver static content - to do so use:

但是,您也可以提供静态内容 - 为此使用:

app.use(express.static(__dirname + '/public'));

Now everything in the /publicdirectory of your project will be delivered as static content at the root of your site e.g. if you place default.htmin the public folder if will be available by visiting /default.htm

现在/public,您项目目录中的所有内容都将作为站点根目录下的静态内容进行交付,例如,如果您将其default.htm放在公共文件夹中,则可以通过访问/default.htm

Take a look through the express APIand Connect Static middlewaredocs for more info.

查看express APIConnect Static 中间件文档了解更多信息。

回答by Daniel

You need to define a root route.

您需要定义一个根路由。

app.get('/', function(req, res) {
  // do something here.
});

Oh and you cannot specify a file within the express.static. It needs to be a directory. The app.get('/'....will be responsible to render that file accordingly. You can use express' render method, but your going to have to add some configuration options that will tell express where your views are, traditionally within the app/views/folder.

哦,您不能在express.static. 它需要是一个目录。该app.get('/'....负责将相应地呈现该文件。您可以使用 express 的 render 方法,但是您必须添加一些配置选项来告诉 express 您的视图所在的位置,传统上是在app/views/文件夹中。

回答by CopyLeft

I had the same problem, so here's what I came up with. This is what my folder structure looked like when I ran node server.js

我有同样的问题,所以这就是我想出的。这是我运行时文件夹结构的样子node server.js

app/
  index.html
  server.js

After printing out the __dirnamepath, I realized that the __dirnamepath was where my server was running (app/).

打印出__dirname路径后,我意识到该__dirname路径是我的服务器运行的地方 ( app/)。

So, the answer to your question is this:

所以,你的问题的答案是这样的:

If your server.jsfile is in the same folder as the files you are trying to render, then

如果您的server.js文件与您尝试渲染的文件位于同一文件夹中,则

app.use(express.static(__dirname + '/default.htm'));

should actually be

实际上应该是

app.use(express.static(__dirname));


The only time you would want to use the original syntax that you had would be if you had a folder tree like so:

如果你有一个像这样的文件夹树,你唯一想要使用你拥有的原始语法的时候:

app/
  index.html
server.js

where index.htmlis in the app/directory, whereas server.jsis in the root directory (i.e. the same level as the app/directory).

whereindex.htmlapp/目录中,而server.js在根目录中(即与目录相同的级别app/)。

Overall, your code couldlook like:

总的来说,您的代码可能如下所示:

var express = require('express');

var app = express();

app.use(express.static(__dirname));

app.listen(process.env.PORT);

回答by PTD

I found myself on this page as I was also receiving the Cannot GET/message. My circumstances differed as I was using express.static()to target a folder, as has been offered in previous answers, and not a file as the OP was.

我发现自己在此页面上,因为我也收到了该Cannot GET/消息。我的情况因我express.static()用于定位文件夹而有所不同,正如之前的答案中所提供的那样,而不是像 OP 那样的文件。

What I discovered after some digging through Express' docsis that express.static()defines its index file as index.html, whereas my file was named index.htm.

我通过一些挖后发现快递的文档express.static()定义了索引文件index.html,而我的文件被命名index.htm

To tie this to the OP's question, there are two options:

要将其与 OP 的问题联系起来,有两种选择:

1: Use the code suggested in other answers

1:使用其他答案中建议的代码

app.use(express.static(__dirname));

and then rename default.htmfile to index.html

然后将default.htm文件重命名为index.html

or

或者

2: Add the indexproperty when calling express.static()to direct it to the desired index file:

2:index调用时添加属性express.static(),将其指向所需的索引文件:

app.use(express.static(__dirname, { index: 'default.htm' }));

回答by beemaster

I've noticed that I forgot the "slash" in the beginning of the Route as below and I was getting same error :

我注意到我忘记了路线开头的“斜线”,如下所示,我遇到了同样的错误:

Wrong :

错误的 :

app.get('api/courses',  (req, res) => { ... }
)

Correct :

正确的 :

  app.get('/api/courses',  (req, res) => { ... }
    )

回答by Serdar Dogruyol

Where is your get method for "/"?

“/”的get方法在哪里?

Also you cant serve static html directly in Express.First you need to configure it.

你也不能直接在 Express 中提供静态 html。首先你需要配置它。

app.configure(function(){
  app.set('port', process.env.PORT || 3000);  
  app.set("view options", {layout: false});  //This one does the trick for rendering static html
  app.engine('html', require('ejs').renderFile); 
  app.use(app.router);

});

Now add your get method.

现在添加您的 get 方法。

app.get('/', function(req, res) {
  res.render('default.htm');
});

回答by Amal Augustine Jose

I had the same issue. Solved it by small changes like below.

我遇到过同样的问题。通过像下面这样的小改动解决了它。

var express = require('express');
var app = express();
app.use(express.static(path.join(__dirname, 'public')));

Got help from here (ExpressJS Documentation - Serving static files).

这里得到帮助(ExpressJS 文档 - 提供静态文件)。

回答by Wambua Makenzi

var path = require('path');

Change app.use(express.static(__dirname + '/default.htm'));to app.use(express.static(path.join(__dirname + '/default.htm')));.

更改 app.use(express.static(__dirname + '/default.htm'));app.use(express.static(path.join(__dirname + '/default.htm')));

Also, make sure you point it to the right path of you default.html.

另外,请确保将其指向 default.html 的正确路径。

回答by Chaim Eliyah

In my case, the static content was already being served:

就我而言,静态内容已经提供:

app.use('/*', express.static(path.join(__dirname, '../pub/index.html')));

...and everything in the app seemed to rely on that in some way. (pathdep is require('path'))

...应用程序中的所有内容似乎都以某种方式依赖于此。(path深度是require('path')

So, a) yes, it can be a file; and b) you can make a redirect!

所以,a) 是的,它可以是一个文件;b) 您可以进行重定向

app.get('/', function (req, res) { res.redirect('/index.html') });

Now anyone hitting /gets /index.htmlwhich is served statically from ../pub/index.html.

现在,任何人点击 get//index.html../pub/index.html.

Hope this helps someone else.

希望这对其他人有帮助。

回答by Ashwani Panwar

You need to restart the process if app.getnot working. Press ctl+cand then restartnode app.

如果app.get不起作用,您需要重新启动该过程。按ctl+c然后restart节点应用程序。