在 node.js 中尝试连接到 localhost:8080 时出现“无法获取 /”

eva*_*kes 5 javascript node.js backbone.js

我正在使用 node.js 作为后端服务器开发backbone.js 应用程序。该应用程序基于 twitter API。当向localhost:8080我请求时,我在浏览器中收到错误“无法 GET /”,我不知道是怎么回事。我的代码如下:

服务器.js

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

var Twit = require('twit')

var client = null;


function connectToTwitter(){
   client = new Twit({
      consumer_key:         'xxx'
    , consumer_secret:      'xxx'
    , access_token:         'xxx'
    , access_token_secret:  'xxx'
  });
}

//get the app to connect to twitter.
connectToTwitter();

var allowCrossDomain = function(req, response, next) {
response.header('Access-Control-Allow-Origin', "http://localhost");
response.header('Access-Control-Allow-Methods', 'OPTIONS, GET,PUT,POST,DELETE');
response.header('Access-Control-Allow-Headers', 'Content-Type');

if ('OPTIONS' == req.method) {
  response.send(200);
}
else {
  next();
}
};

app.configure(function() {
    app.use(allowCrossDomain);
  //Parses the JSON object given in the body request
    app.use(express.bodyParser());
});

//Start server
var port = 8080;
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)

一般目录结构:

C:\Users\eva\Desktop\twitter application\server\server.js 
                                        \client\index.html 
                                                \js
Run Code Online (Sandbox Code Playgroud)

有人能告诉我为什么会发生这个错误吗?

van*_*m23 5

您需要实现哪些服务器必须响应/请求。例如:

app.get('/', function(req, res, next) {
    res.send("Hello world");
});
Run Code Online (Sandbox Code Playgroud)

没有它,express不知道如何处理/