无法GET/Nodejs错误

tom*_*456 11 javascript routes node.js express

我正在使用这里找到的教程:http://addyosmani.github.io/backbone-fundamentals/#create-a-simple-web-server并添加了以下代码.

// Module dependencies.
var application_root = __dirname,
express = require( 'express' ), //Web framework
path = require( 'path' ), //Utilities for dealing with file paths
mongoose = require( 'mongoose' ); //MongoDB integration

//Create server
var app = express();

// Configure server
app.configure( function() {
//parses request body and populates request.body
app.use( express.bodyParser() );

//checks request.body for HTTP method overrides
app.use( express.methodOverride() );

//perform route lookup based on url and HTTP method
app.use( app.router );

//Where to serve static content
app.use( express.static( path.join( application_root, 'site') ) );

//Show all errors in development
app.use( express.errorHandler({ dumpExceptions: true, showStack: true }));
});

//Start server
var port = 5000;
app.listen( port, function() {
console.log( 'Express server listening on port %d in %s mode', port, app.settings.env );
});
Run Code Online (Sandbox Code Playgroud)

启动服务器后,node server.js我收到一条错误说明Cannot GET /我何时访问localhost:5000,我只是想知道是否有人知道这个错误因为Express和Node对我来说是新手?

Jon*_*les 17

我认为你错过了你的路线,你需要定义至少一条路线,例如'/'来索引.

例如

app.get('/', function (req, res) {
  res.render('index', {});
});
Run Code Online (Sandbox Code Playgroud)

  • 你是对的 - 这是一个路线问题,但指南建议此时无需添加路线即可正常工作。 (3认同)

小智 6

您检查过文件夹结构了吗?在我看来,Express找不到您的根目录,该根目录应该是默认目录下一个名为“ site”的文件夹。根据教程,这是它的外观:

node_modules/
  .bin/
  express/
  mongoose/
  path/
site/
  css/
  img/
  js/
  index.html
package.json
Run Code Online (Sandbox Code Playgroud)

例如,在我的机器上,将“ site”文件夹重命名为其他名称时,我开始遇到与您相同的错误。因此,我建议您检查一下“ site”文件夹中是否有index.html页面,该文件夹与server.js文件位于同一路径。

希望有帮助!


小智 5

就像 leonardocouza 一样,我也遇到了同样的问题。为了澄清一点,这就是我运行时的文件夹结构node server.js

node_modules/
app/
  index.html
  server.js
Run Code Online (Sandbox Code Playgroud)

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

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

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

app.use( express.static( path.join( application_root, 'site') ) );
Run Code Online (Sandbox Code Playgroud)

实际上应该是

app.use(express.static(application_root));
Run Code Online (Sandbox Code Playgroud)

唯一一次您想要使用原来的语法是如果您有一个像这样的文件夹树:

app/
  index.html
node_modules
server.js
Run Code Online (Sandbox Code Playgroud)

其中index.html是在app/目录中,而server.js是在根目录中(即与目录同级app/)。

旁注:path您可以使用语法来连接路径,而不是application_root + 'site'调用该实用程序。

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

// Module dependencies.
var application_root = __dirname,
express = require( 'express' ), //Web framework
mongoose = require( 'mongoose' ); //MongoDB integration

//Create server
var app = express();

// Configure server
app.configure( function() {

    //Don't change anything here...

    //Where to serve static content
    app.use( express.static( application_root ) );

    //Nothing changes here either...
});

//Start server --- No changes made here
var port = 5000;
app.listen( port, function() {
    console.log( 'Express server listening on port %d in %s mode', port, app.settings.env );
});
Run Code Online (Sandbox Code Playgroud)


liv*_*ove 5

如果您收到此错误,可能是因为您没有为获取定义路由。

例如:

const express = require('express');

const app = express();

app.get('/people', function (req, res) {
    res.send('hello');
})

app.listen(3000);


http://http://localhost:3000/people --> this works
http://http://localhost:3000 --> this will output Cannot GET / message.
Run Code Online (Sandbox Code Playgroud)