Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法获取/Nodejs错误_Javascript_Node.js_Express_Routes - Fatal编程技术网

Javascript 无法获取/Nodejs错误

Javascript 无法获取/Nodejs错误,javascript,node.js,express,routes,Javascript,Node.js,Express,Routes,我正在使用这里找到的教程:并添加了以下代码 // 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

我正在使用这里找到的教程:并添加了以下代码

// 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 );
});

在使用
node server.js启动服务器后,当我访问
localhost:5000
时,我收到一个错误,说明
无法获取
,我只是想知道是否有人知道这个错误,因为Express和node对我来说都是新手?

我想你错过了你的路线,您需要至少定义一条路由,例如“/”以建立索引

e、 g


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

node_modules/
  .bin/
  express/
  mongoose/
  path/
site/
  css/
  img/
  js/
  index.html
package.json
例如,在我的机器上,当我将“站点”文件夹重命名为其他文件夹时,我开始出现与您相同的错误。因此,我建议您检查index.html页面是否位于与server.js文件路径相同的“site”文件夹中


希望有帮助

就像莱昂纳多·索萨一样,我也有同样的问题。澄清一下,这就是我运行
node server.js

node_modules/
app/
  index.html
  server.js
打印出
\uuuuu dirname
路径后,我意识到
\uuuu dirname
路径就是我的服务器运行的地方(
app/

那么,你的问题的答案是:

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

app.use( express.static( path.join( application_root, 'site') ) );
实际上应该是

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

如果您有这样一个文件夹树,那么您唯一想使用原始语法的时候就是:

app/
  index.html
node_modules
server.js
其中
index.html
位于
app/
目录中,而
server.js
位于根目录中(即与
app/
目录相同的级别)

旁注:在调用
路径
实用程序之前,您可以使用语法
应用程序根+站点
加入路径

总的来说,您的代码可能看起来像:

// 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 );
});

如果出现此错误,可能是因为没有为get定义路由

例如:

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.

你说得对-这是一个路由问题,但指南建议此时不添加路由就可以了。这取决于静态内容文件夹(即站点)中的内容。如果您有一个index.html,如指南中的示例所示,那么它可能会起作用。我确实有一个html文件-不管怎么说,现在工作,该指南中显然还有其他内容,因为这表明当您转到
localhost:5000
时,您会得到一个完全开发的html结果,这在你列出的代码中找不到。是的,这是从前面开始的,但与我遇到的问题无关。
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.