“无法 GET /”在 Node.js 上连接

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

"Cannot GET /" with Connect on Node.js

httpnode.jsconnect

提问by Nathan Campos

I'm trying to start serving some static web pages using connectlike this:

我正在尝试使用以下方法开始提供一些静态网页connect

var connect = require("connect");
var nowjs = require("now");
var io = require("socket.io");


var app = connect.createServer(
  connect.static(__dirname + '/public')
);

app.listen(8180);

So I added a simple index.htmlat the /publicdirectory on the same directory as the app.jsfile is, but when I try to view the page on my browser I get this response from node:

因此,我index.html在与/public文件所在目录相同的目录中添加了一个简单的目录app.js,但是当我尝试在浏览器上查看页面时,我从节点收到此响应:

Cannot GET /

不能获取 /

What I'm doing wrong and how I can correct it?

我做错了什么以及如何纠正它?

采纳答案by Vadim Baryshev

This code should work:

此代码应该工作:

var connect = require("connect");

var app = connect.createServer().use(connect.static(__dirname + '/public'));

app.listen(8180);

Also in connect 2.0 .createServer() method deprecated. Use connect() instead.

同样在 connect 2.0 .createServer() 方法中已弃用。请改用 connect()。

var connect = require("connect");

var app = connect().use(connect.static(__dirname + '/public'));

app.listen(8180);

回答by Serj Sagan

You may be here because you're reading the Apress PRO AngularJSbook...

你来这里可能是因为你正在读这Apress PRO AngularJS本书......

As is described in a comment to this questionby KnarfaLingus:

正如在一个注释说明了这个问题KnarfaLingus

[START QUOTE]

[开始报价]

The connect module has been reorganized. do:

连接模块已重新组织。做:

npm install connect 

and also

并且

npm install serve-static

Afterward your server.jscan be written as:

之后你server.js可以写成:

var connect = require('connect');
var serveStatic = require('serve-static'); 
var app = connect(); 

app.use(serveStatic('../angularjs')); 

app.listen(5000);

[END QUOTE]

[结束语]

Although I do it, as the book suggests, in a more concise way like this:

虽然我这样做,正如书中所暗示的那样,以一种更简洁的方式:

var connect = require('connect');
var serveStatic = require('serve-static');

connect().use(
    serveStatic("../angularjs")
).listen(5000);

回答by Stuart Hallows

You'll see the message Cannot GET /if you don't specify which page it is that you're trying to get, in other words if your URL is something like http://localhost:8180. Make sure you enter a page name, e.g. http://localhost:8180/index.html.

你会看到消息Cannot GET /,如果你不指定它是哪个页面,你正在试图获得,换句话说,如果您的网址是一样的东西http://localhost:8180。确保输入页面名称,例如http://localhost:8180/index.html.

回答by GP Van Eron

Had the same issue. It was resolved as described above.

有同样的问题。如上所述已解决。

In my index.js

在我的 index.js 中

var port = 1338,
express = require('express'),
app = express().use(express.static(__dirname + '/')),
http = require('http').Server(app),
io = require('socket.io')(http);

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

io.on('connection', function(socket){
    console.log('a user connected');
});

http.listen(port, function(){
    console.log("Node server listening on port " + port);
});

and in my index.html

在我的 index.html 中

<!doctype html>
<html>
    <head>
        <title>
            My page
        </title>
    </head>
    <body>
        <script src = "lib/socket.io.js"></script>
        <script src = "lib/three.js"></script>
        <script>
            var socket = io();
        </script>
    </body>
</html>

the three.js was just in there for path testing. This will set all child files to start at the root directory of your app. Also socket.io.js can be called automatically using <script src = "/socket.io/socket.io.js">through some dark magic (since there is physically a node_modules and lib directory in between) .

Three.js 只是在那里进行路径测试。这会将所有子文件设置为从应用程序的根目录开始。此外,socket.io.js 可以<script src = "/socket.io/socket.io.js">通过一些黑暗魔法自动调用(因为物理上有一个 node_modules 和 lib 目录)。

回答by prashantsahni

var connect = require('connect');
var serveStatic = require('serve-static');
var app = connect(); 
app.use(serveStatic('../angularjs'),  {default: 'angular.min.js'}); app.listen(3000); 

回答by Wojciech Fornal

You may also want to try st, a node module for serving static files. Setup is trivial.

您可能还想尝试st,一个用于提供静态文件的节点模块。设置很简单。

npm install connect

npm install st

And here's how my server-dev.js file looks like:

这是我的 server-dev.js 文件的样子:

var connect = require('connect');
var http = require('http');
var st = require('st');

var app = connect()
    .use(st('app/dev'));

http.createServer(app).listen(8000);

or (with cache disabled):

或(禁用缓存):

var connect = require('connect');
var http = require('http');
var st = require('st');

var app = connect();

var mount = st({
  path: 'app/dev',
  cache: false
});

http.createServer(function (req, res) {
  if (mount(req, res)) return;
}).listen(8000);

app.use(mount);

回答by Gerard Lanphear

The solution to "Cannot Get /" can usually be determined if you do an "ng build" in the command line. You will find most often that one of your "imports" does not have the correct path.

如果您在命令行中执行“ng build”,通常可以确定“无法获取/”的解决方案。您经常会发现您的“导入”之一没有正确的路径。

回答by Sesha Kiran

The easiest way to serve static files is to use "harp". It can be found here. You can serve up your files from the location you want via node is:

提供静态文件的最简单方法是使用“竖琴”。在这里能找到它。您可以通过节点从您想要的位置提供您的文件:

var harp = require("harp")
harp.server(projectPath [,args] [,callback])

Hope this helps.

希望这可以帮助。

回答by Ashwani Panwar

You might be needed to restartthe process if app.getnotworking. Press ctl+cand then restart node app.

restart如果app.getnot工作,您可能需要参与该过程。按ctl+c,然后按restart node app

回答by Tarun Gupta

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 /public directory of your project will be delivered as static content at the root of your site e.g. if you place default.htm in 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 middleware docs for more info.

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