2015-06-02 65 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(); 

干杯!