简体   繁体   中英

How Can I Use Node.js With Cappuccino?

I'd like to have a client application implemented in Cappuccino that uses Node.js as the server.

I have currently got Node running with Express :

var express = require( 'express' );

var app = express();

app.get( '/an_endpoint', function(req, res){

    res.send('Hello From Node Express!\n');

});

app.listen(1337);

Which is verifiable with:

$ node hello_echo.js
$ curl http://127.0.0.1:1337/an_endpoint
> Hello From Node Express!

As far as the client code, it's a simple little app, with a button that does this when clicked:

// in did finish launching
[button setTitle:"Ping Node"];
[button setTarget:self];
[button setAction:@selector(doPing:)];

- (void)doPing:(id)sender
{
    var connection = [CPURLConnection connectionWithRequest:[CPURLRequest requestWithURL:'http://127.0.0.1:1337/an_endpoint/'] delegate:self];
}

- (void)connection:(CPURLConnection) connection didReceiveData:(CPString)data
{
    alert('Node Says: ' + data);
}

- (void)connection:(CPURLConnection)connection didFailWithError:(CPString)error
{
    alert('Error: ' + error);
}

When I load the app ( from http://127.0.0.1:8080/NewApplication/index.html ) and click the button, in Google Chrome, on OS X, I get the following errors in the console, the first due to the latter:

OPTIONS http://127.0.0.1:1337/an_endpoint/ 404 (Not Found) Objective-J.js:716
        XMLHttpRequest cannot load http://127.0.0.1:1337/an_endpoint/. 
Origin http://127.0.0.1:8080 is not allowed by Access-Control-Allow-Origin.

Which, is obviously due to the fact that my node server is at :1337, and my Cappuccino app is at :8080, which are technically different domains, due to the port part.

A bit of research led me to this post, which recommends considering the use of JSONP to inject remote scripts into the app. It sounds very messy, so I'd like to not go that route if not necessary.

So, my question is, how can I allow Cappuccino and Node.js to work together in harmony? It seems that if I can tell the Cappuccino app to use this ( header("Access-Control-Allow-Origin", "*"); ) header, all should be well, but I'm not sure how to do that. I tried having Node send that header, and it didn't seem to do anything.

您应该使用节点来提供Cappuccino应用程序,因此它们都在同一个端口上。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM