简体   繁体   English

express.static:无法获取/

[英]express.static : Cannot GET /

I'm learning how to use node.js on this blog/website: http://blog.modulus.io/absolute-beginners-guide-to-nodejs 我正在此博客/网站上学习如何使用node.js: http//blog.modulus.io/absolute-beginners-guide-to-nodejs

Everything worked fine until I got to the part Doing Something Useful - Express 一切正常,直到我到了部分有某些用处- 快递

Express seemed to install fine with npm. Express似乎可以与npm一起安装。

Here's my code: 这是我的代码:

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

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

app.listen(8080);

I put that into a file called test.js and put it in a folder called Public . 我将其放入名为test.js的文件中,并将其放入名为Public的文件夹中。 In that folder I also added a html doc with just a few words. 在该文件夹中,我还添加了一个仅带几个单词的html文档。

On the browser ´ http://localhost:8080 ´ shows up with Cannot GET / and ´ http://localhost:8080/index.html ´ shows Cannot GET /index.html 在浏览器上,“ http:// localhost:8080 ”显示为Cannot GET /,而在http:// localhost:8080 / index.html上显示为Cannot GET /index.html

You need to put the public folder on the same level as your test.js file, like this 您需要将公用文件夹放置在与test.js文件相同的级别上,如下所示

.
├── Public
│   └── index.html
└── test.js

1 directory, 2 files

It seems that you put your test.js in your Public directory. 似乎您将test.js放在了Public目录中。

__dirname means the name of the directory of the current file, that is why your Public directory should be in the same folder than your js file. __dirname表示当前文件目录的名称,这就是为什么Public目录应与js文件位于同一文件夹中的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM