I'm new to programming and for this project I'm building a web app using Parse.com Cloud Code and Express. The structure looks like this:
- app.js (Express related codes)
- views/hello.ejs (template)
- main.js (cloud functions)
I also have an iOS app that can post a link on Facebook that looks like this: myapp.parseapp.com/hello?objectid. When Facebook users click that link and get redirected to the web app, the web app will automatically res.render the template based on the provided objectid. The objectid is then used to get some data, which will be displayed on the page, from a Parse class using Parse.Query, but my obstacle is not about Parse.Query.
To check if objectid is successfully captured by the server, I tried the following codes to render the template and insert the objectid into the placeholder for the text variable, but the put objectid here is not changed to the provided objectid when page is loaded. Can anyone tell me what went wrong, or if I should do this in another approach?
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();