2016-06-14 67 views
0

我正在使用parse-server,並且想要像下面的示例那樣使用nodejs和cloudCode。parse-server cloudCode with nodejs

這是例如: Adding nodejs to Parse

這裏是從鏈路的示例代碼

var http = require('http'); 
var express = require('express'); 
var bodyParser = require('body-parser'); 
var ParseCloud = require('parse-cloud-express'); 
var Parse = ParseCloud.Parse; 
var app = express(); 
// Host static files from public/ 
app.use(express.static(__dirname + '/public')); 
// Define a Cloud Code function: 
Parse.Cloud.define('hello', function(req, res) { 
    res.success('Hello from Cloud Code on Node.'); 
}); 
// Mount the Cloud Code routes on the main Express app at /webhooks/ 
// The cloud function above will be available at /webhooks/function_hello 
app.use('/webhooks', ParseCloud.app); 
// Launch the HTTP server 
var port = process.env.PORT || 80; 
var server = http.createServer(app); 
server.listen(port, function() { 
    console.log('Cloud Code on Node running on port ' + port + '.'); 
}); 

的console.log(process.env.PORT);

我已經導入了所有需要的模塊,不過,當我運行的服務器,並嘗試去鏈接「127.0.0.1/webhooks/function_hello」我回來Cannot GET /webhooks/function_hello

任何建議?

* OUTPUT當我運行該腳本*

undefined 
Cloud Code on Node running on port 80. 

UPDATE似乎與他們改變支持地位cloudcode解析的關閉影響使用的NodeJS它整合

+0

您可以在執行服務器時添加控制檯的輸出嗎? – Duane

+0

我已加入 – user3676224

+0

嘗試發帖127.0.0.1:80/webhooks/functions/hello。 – ChunTingLin

回答

1

有同樣的問題。 GET在這裏不起作用。您需要製作一個POST請求,然後您將獲得{"success":"Hello from Cloud Code on Node."}

0

請確保您正在運行正確的腳本node SCRIPT_NAME

看起來你的快遞服務器設置爲端口5000.

參見:var port = process.env.PORT || 5000;

您的網址更改爲http://127.0.0.1:5000/webhooks/function_hellolocalhost:5000/webhooks/function_hello,它應該出現

如果你想默認端口(80),你將需要與sudo運行腳本,使上運行改變了代碼。

var port = process.env.PORT || 80;

的文件夾添加到您的命名public目錄。在該文件夾內放置一個名爲index.html的文件。在該文件中輸入Hello World,將其保存。重新啓動您的服務器。看看你是否可以打開http://127.0.0.1/

+0

我已經確保我正在運行正確的腳本,並且我嘗試了80端口,正如您所建議的那樣。但是http:// 127 ...和localhost:80..etc都沒有返回除「Can not GET/webhooks/function_hello」 – user3676224

+0

@ user3676224以外的結果,我已經更新了包含端口更改的說明。如果這不起作用,請告訴我console.log(process.env.PORT)的輸出。 – Duane

+0

它導致「未定義」,輸出是「undefined 端口80上運行的節點上的Cloud Code」。 **更新**我只是想告訴你我是如何放置它「var port = process.env.PORT || 80; var server = http.createServer(app); server.listen(port,function(){ ('端口上運行的節點上的雲代碼+端口+'。'); }); console.log(process.env。PORT);「 – user3676224