I am working on backbone.js application with node.js as backend server.
the application is based on twitter API. When requesting to localhost:8080
I got the error "Cannot GET /" in the browser and I do not know what is the matter. My code is as follows:
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 );
});
Directory structure in general:
C:\Users\eva\Desktop\twitter application\server\server.js
\client\index.html
\js
Can someone tell me why this error occur?
You need to access your app with http in the URL not https when developing locally. You may have your web browser set to automatically try to upgrade the connection from http to https. If you, disable this setting.
If you're unable to access the web server via localhost, there's a chance that your firewall may be blocking the connection. You can often resolve this problem by modifying your firewall settings to allow incoming connections for the port that MAMP is trying to access.
You need to implement what server must response on /
requests. For example:
app.get('/', function(req, res, next) {
res.send("Hello world");
});
Without it, express
doesn't know how to handle /
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With