2015-06-02 64 views
2

我是編程和新項目的新手,我正在使用Parse.com Cloud Code和Express構建一個Web應用程序。該結構是這樣的:
基於URL參數顯示動態網頁內容(Parse Cloud,Express,ejs)

  • app.js(快遞相關代碼)
  • 的意見/ hello.ejs(模板)
  • main.js(雲功能)

我還有一個iOS應用可以在Facebook上發佈鏈接,如下所示:myapp.parseapp.com/hello?objectid。當Facebook用戶點擊該鏈接並重定向到網絡應用程序時,網絡應用程序將自動根據所提供的objectid模板res.render。然後使用objectid從Parse類使用Parse.Query獲取一些數據,這些數據將顯示在頁面上,但我的障礙不在於Parse.Query。

要檢查是否objectid成功地通過服務器所捕獲,我嘗試下面的代碼來呈現模板,並插入objectid到佔位符text變量,但put objectid here當頁面加載不改變所提供的objectid 。任何人都可以告訴我哪裏出了問題,或者如果我應該用另一種方法做到這一點?

hello.ejs (template):

<!DOCTYPE html> 
<html> 
    <head> 
    <title>My Web App</title> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
    <script type="text/javascript"> 
     $.post('/hello', {oid: window.location.toString().split("?")[1]}); 
    </script> 
    </head> 
    <body> 
    <p><%= text %></p> 
    </body> 
</html> 

app.js (express):

// Initialize Express in Cloud Code. 
var express = require('express'); 
var app = express(); 

// Global app configuration section 
app.set('views', 'cloud/views'); // Specify the folder to find templates 
app.set('view engine', 'ejs'); // Set the template engine 
app.use(express.bodyParser()); // Middleware for reading request body 

// Render view 
app.get('/hello', function(req, res) { 
    res.render('hello', { text: 'put objectid here' }); 
}); 

app.post('/hello', function(req, res) { 
    res.render('hello', { text: req.body.oid }); 
}); 

app.listen(); 
+0

我在另一篇文章中找到了解決方案:http://stackoverflow.com/questions/20089582/how-to-get-url-parameter-in-express-node-js – Pork

回答

0

一些調查研究後,我遇到了這個帖子:how to get url parameter in express node js。決定試一試,它是成功的!這是解決方案。

網址更改爲:myapp.parseapp.com/hello?id=objectid

<head><script>不再需要。最終的代碼如下所示:

hello.ejs (template):

<!DOCTYPE html> 
<html> 
    <head> 
    <title>My Web App</title> 
    </head> 
    <body> 
    <p><%= text %></p> 
    </body> 
</html> 

app.js (express):

// Initialize Express in Cloud Code. 
var express = require('express'); 
var app = express(); 

// Global app configuration section 
app.set('views', 'cloud/views'); // Specify the folder to find templates 
app.set('view engine', 'ejs'); // Set the template engine 
app.use(express.bodyParser()); // Middleware for reading request body 

// Render view 
app.get("/hello", function(req, res) { 
    res.render('hello', { text: req.param("id") }); 
}); 

app.listen(); 

乾杯!