parse-Server cloudCode with nodejs - node.js

I am using parse-server and I want to use nodejs with cloudCode as in the example below.
This is the example:
Adding nodejs to Parse
here is the example code from the link
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);
I have imported all the required modules, but still, when I run the server and try to go to the link "127.0.0.1/webhooks/function_hello" I get back Cannot GET /webhooks/function_hello
Any advise?
*OUTPUT when i run the script *
undefined
Cloud Code on Node running on port 80.
UPDATE it seems that with parse's shutdown that they have changed support status for cloudcode which affects integrating it with NodeJs

Had the same issue. GET doesn't work here. You need to make a POST request, and then you'll get {"success":"Hello from Cloud Code on Node."}

Please make sure you are running the right script with node SCRIPT_NAME
It appears your express server is set to port 5000.
See: var port = process.env.PORT || 5000;
Change your URL to http://127.0.0.1:5000/webhooks/function_hello or localhost:5000/webhooks/function_hello and it should appear
If you want to run on the default port (80) you will need to run with sudo for your script and make the following change to the code.
var port = process.env.PORT || 80;
Add a folder to your directory named public. Inside that folder place a file named index.html. Type Hello World in that file, save it. Restart your server. See if you can open http://127.0.0.1/.

Related

Openshift not setting environment variable for node project

I have a simple node project that I am trying to get deployed to Openshift Online 3 but having some ip and port problems.
Reading the documentation I need to get some environment variables from openshift system, seems legit. But Openshift doesn't seem to set the variables OPENSHIFT_NODEJS_PORT and OPENSHIFT_NODEJS_IP.
Using the example below:
var express = require('express');
var app = express();
var server_ip_address = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1'
var port = process.env.OPENSHIFT_NODEJS_PORT || 1212
console.log('environment_port: ' + process.env.OPENSHIFT_NODEJS_PORT);
console.log('environment_ip: ' + process.env.OPENSHIFT_NODEJS_IP);
app.listen(port, server_ip_address, function (err) {
console.log('Running on port ' + port + ' ip: ' + server_ip_address);
});
I get the following output from openshift logs:
Why isn't Openshift Online 3 setting the system varaibles?
In OpenShift 3, no environment variables will be set. Your HTTP server process should listen on port 8080 if using one of the S2I builders.

Use variable subdomains for routing with wildcard

I want to create an express application that uses dynamic/variable subdomains for routing. This is what I want to do:
http://<username>.mysite.dev should forward the requests to the users/index.js
In the users/index.js I will access the username via req.subdomain[0]. It would be nice if I could also run an regular expression check on <username>.
My first approach:
I am using the node package express-subdomain to route the requests:
/* import node packages */
var express = require('express'),
subdomain = require('express-subdomain');
/* application config */
var app = express(),
port = process.env.PORT || 3000;
/* import all apps */
var users = require('./users/index.js');
/* route requests by subdomain */
app.use(subdomain('*', users));
app.get('/', function(req,res) {
/* Never get here */
res.send('Homepage');
});
/* run app on given port */
app.listen(3000, function() {
console.log('Listening on port ' + port + ' ...');
});
The problem with this approach is that the * is not working properly. It forwards all requests to my users/index.js even when there is no subdomain (http://mysite.dev). One solution for this problem would be, if I change the routing like this:
app.use(subdomain('*.users', users));
So I can access the users/index.js through http://<user>.users.mysite.dev and I can also reach the normal site, when there is no subdomain. But this approach is not really what I want - the users subdomain is too much. In addition I can not use regex.
Now, I am searching for a better solution for this problem.

Node JS express app not serving index file on Openshift. Works on local

I've done some updates to my node app, deployed to OpenShift and now it wont send the index.html file when live. I updated Express too and fixed all the errors from there...
My Directory structure is as follows:
/Site/
....server.js
....app/
........index.html/
My server looks like this:
var express = require('express');
var app = express();
var path = require('path');
app.use(express.static('app'));
require('./server-stripe.js')(app);
app.get('*', function(req, res) {
res.sendFile(path.resolve(__dirname + 'app/index.html'));
});
var server_port = process.env.OPENSHIFT_NODEJS_PORT || 8080
var server_ip_address = process.env.OPENSHIFT_NODEJS_IP || process.env.OPENSHIFT_INTERNAL_IP ||'127.0.0.1'
app.listen(server_port, server_ip_address, function () {
console.log( "Listening on " + server_ip_address + ", server_port " + server_port )
});
It works fine on my local environment but in Production I get a 503 Service Unavailable.
Any advice would be greatly appreciated! Whits end and all that ;)
Thanks,
Matt
Edit
This is the log:
==> app-root/logs/haproxy.log <==
[WARNING] 009/181452 (443835) : Server express/local-gear is UP (leaving maintenance).
[WARNING] 009/181453 (443835) : Server express/local-gear is DOWN, reason: Layer7 wrong status, code: 404, info: "Not Found", check duration: 36ms. 0 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
[ALERT] 009/181453 (443835) : proxy 'express' has no server available!
==> app-root/logs/nodejs.log <==
Error: ENOENT, stat '/var/lib/openshift/539976e05004467473000668/app-root/runtime/repo/app/index.html'
I'm reading around the hapoxy stuff though I'm not too clued into it?
Haproxy needs your app to serve something with http 200 OK response code, from root path (http://domain.name). Otherwise it will report your gear is DOWN. You may try checking the app with curl from the app's own gear, see the deployment log for IP and port.
To serve the static files from a selected folder (here "/app") without worrying about type recognition, you can use:
app.use(express.static(__dirname + '/app'));
If the native mime types are not enough, you can extend them:
express.mime.type['ogv'] = 'video/ogg';
Otherwise, see Basic static file server in NodeJS for a 'manual' implementation of static file server.
see: http://expressjs.com/api.html

Socket.IO connection error

I've an application in node.js with socket.io. Everything was running completely fine, but suddenly the browser started to send this error.
failed: Error in connection
establishment:net::ERR_TUNNEL_CONNECTION_FAILED
I didn't make any code change.
The protocol used by socket is ws:// and when I try to use this url in browser
'ws://highgarden-nodejs-91180.sae1.nitrousbox.com/socket.io/?EIO=3&transport=websocket&sid=T9Unec8KbWw-GAL8AAAF'
Chrome returns this error:
Failed to load resource: net::ERR_DISALLOWED_URL_SCHEME
This is a part of the socket setup code:
server.js:
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);
var port = process.env.PORT || 3000
*------------------------------------------*
// routes ===
var routes = require('./config/routes.js');
var sock = {}
routes(app, passport, sock);
io.sockets.on('connection', sock.update);
// launch ===
server.listen(port);
Thanks advance.
Hi the exception ERR_TUNNEL_CONNECTION_FAILED happens when a tunnel connection through the proxy could not be established. And the ERR_DISALLOWED_URL_SCHEME happens when the scheme of the URL is disallowed.
May you need use it behind the proxy!
Chrome 45.0.2454.101 m says the page has been disabled or moved on the server.

Can't get index.html to show with Express in Nodejs

I'm trying to run my first express app, but can't seem to get my webpage to show. I have the following code:
var fs = require("fs");
var config = JSON.parse(fs.readFileSync("files/config.json"));
var host = config.host;
var port = config.port;
var express = require("express");
var app = express();
app.use(app.router);
app.use(express.static(__dirname + "/public"));
app.get("/", function(request, response){
response.send("hello!");
});
app.listen(port, host);
console.log("Listening on port" + port);
Here is my directory tree
nodejs/
js/
javascript.js
public/
index.html
I know the server is running because I get my "Hello!" response in the browser when I run 127.0.0.01:1337
But when I try and type the webpage 1227.0.0.1:1337/index.html, I get Cannot GET /index.html displayed in the browser
So I'm guessing it's something wrong with the name value in my get method, but can't figure out what it is and how to fix it.
Your app will only route page requests that are set up at the time of your app.use(app.router) call. So reorder your app.use calls to be one of the following:
Express 3
app.use(express.static(__dirname + "/public"));
app.use(app.router);
__dirname is the directory that the executing script resides in, so because that lives in the js directory that's a peer to public your code would need to be:
app.use(express.static(__dirname + "/../public"));
app.use(app.router);
Express 4
Express 4 removes the need to manually do app.use(app.router). With Express 4 you just need:
app.use(express.static(__dirname + "/../public"));
I had this issue .... after a lot of trouble I find that if you run two workspaces or project at t time then it will create this scenario. so you might open only a workspace at a time and not just file ... open the hole folder then run the specific file.make the following change in your VS code setting.
setup the settings
For 64bit system.
{
"liveServer.settings.AdvanceCustomBrowserCmdLine: ": "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe",
"liveServer.settings.NoBrowser": false
}
For 32bit system.
{
"liveServer.settings.AdvanceCustomBrowserCmdLine: ": "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",
"liveServer.settings.NoBrowser": false
}

Resources