I'm trying to use dispatch but when I do a test in the broswer (http://127.0.0.1/user) I get the following message
Cannot GET /user/
what am I doing wrong?
127.0.0.1 - - [Mon, 14 May 2012 17:40:13 GMT] "GET /user/ HTTP/1.0" 404 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0"
http://www.something.com/api/ping -> works fine
My nginx config file has this entry also.
location /user/ {
proxy_pass http://www.something.com:8181;
}
Any have any suggestions how I can get dispatcher working?
var dispatch = require('connect')
function init() {
var port = process.env.VCAP_APP_PORT || process.env.PORT || parseInt( process.argv[2], 10 )
var staticFolder = __dirname + '/../site/public'
console.log('port='+port+' static='+staticFolder)
var server = connect.createServer(
connect.logger(),
connect.bodyParser(),
connect.cookieParser(),
connect.query(),
connect.session({secret: config.secret}),
dispatch({
'/user': {
GET: api.get_user,
'/socialmsg/:when': {
POST: api.social_msg
}
}
}),
function(req,res,next){
if( 0 === req.url.indexOf('/api/ping') ) {
console.log(req.url+': '+JSON.stringify(req.query))
res.writeHead(200)
res.end( JSON.stringify({ok:true,time:new Date()}) )
}
else next();
},
connect.static( staticFolder )
)
server.listen( port )
}
As per https://github.com/senchalabs/connect, I would significantly refactor your code to make it more readable and more obvious as to what went wrong. In short, I don't believe the static directory seem to be configured as desired. I have not found a createServer() method as per the Connect module (ExpressJS and http/https modules have it though), especially one with 8 parameters as you've provided.
That said, I would stick the documentation's convention as follows:
var connect = require('connect'),
http = require('http');
var staticFolder = __dirname + '/../sites/public';
var server = http.createServer(
connect
.use(connect.static(staticFolder))
.use(connect.logger)
.use(connect.bodyParser)
// etc for each of the 'connect.' middleware you are using above
.use(function (req, res) {
// Request handling here
});
);
server.listen(8181);
In the empty function (req, res) {} you would want to put all the stuff you have for api/ping as well as the equivalents for the /user request.
In your question you provided the 'connect.static()' as an argument for createServer() (i.e. connect.createServer(connect.static)) as opposed to an argument for connect.use(), the middleware handling function in the module.