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

时间:2015-05-01 12:07:51

标签: javascript node.js backbone.js

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

server.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 );
});

一般目录结构:

C:\Users\eva\Desktop\twitter application\server\server.js 
                                        \client\index.html 
                                                \js

有人能告诉我为什么会出现这种错误吗?

1 个答案:

答案 0 :(得分:4)

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

app.get('/', function(req, res, next) {
    res.send("Hello world");
});

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