Вопрос

I have a newbie question. I have a flash facebook aplication, and it uses facebook credits. I use express framework to serve my static html file which contains the application.swf in it.

This is how i configure the express :

var app = express();

app.configure(function(){
    app.use(express.methodOverride());
    app.use(express.bodyParser());
    app.use(express.logger());
    app.use(app.router);

    // Redirections for default pages
    app.all("/", function(req, res) { res.redirect("/index.html"); });
    app.all("/facebook", function(req, res) { res.redirect("/facebook/index.html"); });

    // Serve static files
    app.all("*", express.static('/my/static/files/directory'));
    app.use(express.errorHandler({
        dumpExceptions: true,
        showStack: true
    }));
});

require('http').createServer(app).listen(80);
require('https').createServer({
    key: fs.readFileSync('./certs/www.app.org/www.app.org.key'),
    cert: fs.readFileSync('./certs/www.app.org/www_app_org.crt'),
    ca: fs.readFileSync('./certs/www.app.org/www_app_org.ca-bundle'),
}, app).listen(443);

I use this structure to serve my application on both http and https requests. It works well when the incoming http request type is GET.

However, when a user purchase an item in the app, facebook send a POST request to my application. The problem is express throws 404 error when recieves a POST request to a static file directory.

P.S. : POST request is send to the same url which works very well on GET requests.

Here is the monitoring result :

node_local:httpserver 88.250.59.159 - - [Fri, 17 Aug 2012 11:51:09 GMT] "POST /facebook/index.html HTTP/1.1" 404 - "-" "Apache-HttpClient/4.1.3 (java 1.5)"

node_local:httpserver 88.250.59.159 - - [Fri, 17 Aug 2012 11:50:59 GMT] "GET /facebook/index.html HTTP/1.1" 200 5892 "-" "Apache-HttpClient/4.1.3 (java 1.5)"
Это было полезно?

Решение

It does not matter if you use all for static middleware, it performs a router independent check for request type to see if it is POST or HEAD. So, don't use it in app.all, just place in an app.use call. It should be within the application stack, not router.

You could intercept the request before it goes into static middleware, just add another middleware before static, something like this should suffice:

app.post("/facebook/index.html", function(req, res, next) {
  req.method = "GET";
  next();
});

I did not test this though.

Другие советы

I worked on a project like that, firstly you have to check that "can facebook access your server?". That means your server had to be accessable by internet with port 80 or 8080 .

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top