How to connect to a machine on Cloud9? - cloud9

I opened an account on Cloud9 and I ran some code successfully. I got the following output in the console:
Your code is running at https://****1986.c9users.io.
Important: use process.env.PORT as the port and process.env.IP as the
host in your scripts!
Debugger listening on port 15454
listening on port 3000
The code is:
var express = require('express');
var app = express();
app.get('/', function(req,res) {
res.send("OK");
});
app.listen(process.env.PORT, function() {
console.log("listening on port " + process.env.PORT);
});
So after running it, I opened my browser and surfed to:
https://***1986.c9users.io:3000
I would expect to get a "OK" in the browser, but it seems that the browser can't reach this destination.
What address do I have to type so I can connect my Cloud9 server?

Probably, Cloud9 has a problem with running files inside directories.
This file was placed inside the directory 'server' (when I couldn't access the address by the browser). Once I put it in the main directory, I could connect it by applying to https://***1986.c9users.io (without port number)

Related

How can I tell why my node app is crashing on Heroku? (HTML, Stripe, Heroku, NodeJS)

So, I've developed a website (HTML) that has an embedded payment form from Stripe called Checkout. When you visit the website, it prompts you to enter your credit card information, so the checkout form is working correctly.
The issue I'm having is processing the token once it's created.
I'm extremely new to web development and I've never written server code before so please, bear with me.
I've been following guides (Process payments with Node, Vue, Stripe & How to set up Stripe payments with Node.js) and stripes documentation on tokenization to create charges using server-side code (Stripe Checkout)
I understand that I have to have Heroku set up to process the charges so I created an account and set up an app from my terminal. I made a new directory that has the modules required (stripe, express, and bodyParser) and I have this code in my server.js file:
It deploys to Heroku successfully but crashes. This is what is being returned in the console:
What am I doing wrong? Any assistance would be a great help.
You are missing a vital piece:
// Start the server
app.listen(port, function(){
console.log('Server listening on port ' + port)
});
You don't seem to start the server in your application. This should be in the bottom of server.js. You also have to remember to set the port:
var port = process.env.PORT || 3000;
It goes above app.listen of course.
I can't tell for sure if that will fix all your errors, but you have to start with starting the server first.
Also, remember to check for errors in callbacks. In the callback for create you are not doing that. E.g.
if (err){
console.error(err);
res.json({ error: err, charge: false });
} else {
// send response with charge data
res.json({ error: false, charge: charge });
}
You are doing res.send() whether or not there are errors. I doubt that this has anything to do with the Heroku error though.

BrowserSync with custom URL

Im using BrowserSync for my site. The following live reloads my CSS changes but it opens a webpage at http://localhost:3000/
gulp.task('sass-watch', ['theme-css'], browserSync.reload);
gulp.task('browser-sync', function() {
var files = [
'htdocs/themes/custom/my-theme/dist/*'
];
browserSync.init(files,{
proxy: 'mysite.com'
});
});
My site is configured via Vagrant to be accessed locally at mysite.com. How can I get BrowserSync working at this custom URL?
Ive tried the following as my VM is using port 80.
gulp.task('browser-sync', function() {
var files = [
'htdocs/themes/custom/my-theme/dist/*'
];
browserSync.init(files,{
open: 'external',
host: 'mysite.com',
proxy: 'mysite.com',
port: 80
});
});
However I get an error:
events.js:141
throw er; // Unhandled 'error' event
^
Error: listen EACCES 0.0.0.0:80
at Object.exports._errnoException (util.js:874:11)
at exports._exceptionWithHostPort (util.js:897:20)
at Server._listen2 (net.js:1221:19)
at listen (net.js:1270:10)
at Server.listen (net.js:1366:5)
at module.exports.plugin (/path-to-my-site/node_modules/browser-sync/lib/server/index.js:24:25)
at Object.module.exports.startServer [as fn] (/path-to-my-site/node_modules/browser-sync/lib/async.js:236:52)
at /path-to-my-site/node_modules/browser-sync/lib/browser-sync.js:149:14
at iterate (/path-to-my-site/node_modules/browser-sync/node_modules/async-each-series/index.js:8:5)
at /path-to-my-site/node_modules/browser-sync/node_modules/async-each-series/index.js:16:16
If I use the default port of 3000 then the page loads but I get error connection refused.
ERR_CONNECTION_REFUSED
Your first attempt is the correct one, of course that way you would get browserSync to serve your app on http://localhost:3000, which is the default.
The second one has nothing wrong except that the address you are trying to assign to browserSync is already used by vagrant.
So if you want browserSync to be on mysite.com you should configure vagrant to take something else.
If you do so, then the script becomes:
gulp.task('browser-sync', function() {
var files = [
'htdocs/themes/custom/my-theme/dist/*'
];
browserSync.init(files,{
open: 'external',
host: 'mysite.com',
proxy: 'mylaravel.com',
port: 80
});
});
For anyone else who comes across this question on Google like I did: The "port" option specifies the port you want BrowserSync to listen to, not the port that the server it will be proxying is listening to. The problem with the setup in this question is that it is trying to assign BrowserSync to listen to the same port that Vagrant is already listening to.
If you remove the "port" option, BrowserSync defaults to port 3000, and you will be able to access it successfully at mysite.com:3000. That is roughly the setup I'm currently using. Alternately, you should be able to keep BrowserSync assigned to port 80 if you reassign Vagrant to another port (e.g., 8080), and then access BrowserSync at mysite.com directly.
You are trying to start node as a non-root user. Linux by default only allows root to bind to ports 1024 or below.

Register service worker failed with localhost

I have the following error when I try to register a service worker in a basic app served by a node Express V4 server / on Chrome 42:
DOMException: Failed to register a ServiceWorker: A bad HTTP response
code (404) was received when fetching the script. {message: "Failed to
register a ServiceWorker: A bad HTTP res…code (404) was received when
fetching the script.", name: "NetworkError", code: 19, INDEX_SIZE_ERR:
1, DOMSTRING_SIZE_ERR: 2…} code: 19 message: "Failed to
Here is the register code :
if ('serviceWorker' in navigator){
console.log("SW present !!! ");
navigator.serviceWorker.register('worker.js', {
//scope: '/toto/'
}).then(function(registration){
console.log('Service worker registered : ', registration.scope);
})
.catch(function(err){
console.log("Service worker registration failed : ", err);
});
}
I think You are trying to Register non-existent script. In this case this issue comes. Please check your script path and scope path. Maybe you don't have any 'worker.js' in the directory where this script exists. If this is the case, please provide full path or provide worker.js in same directory.
I also had this error when using an express server. It turns out the problem was with the server setup itself, not the service worker registration code. I had told my express app to get index.html as the default root using:
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname + '/index.html'));
});
However I had not told express the location of any other files I wanted it to be able to use. At this stage my only other file was the service worker file which was sitting at the root of the directory so I fixed the problem by adding this line to the server file:
app.use(express.static(__dirname + '/'));
To debug whether your issue is with the server itself you could download Web Server for Chrome and point it at the root directory of your app. Check the server is started and click on the Web Server URL. If your service worker registration now succeeds you'll know it's a problem with your express server setup not your service worker registration code.

Total node js beginner...how to set up node-mysql?

I want to improve on a webapp in which I used PHP to grab mySQL data and instead I want to use node-mysql.js as I have a .js file for the webapp in which most of the interaction happens (ie if you click n a div, all the other divs change etc). I've never used node.js before so I really don't have any idea where to start - I've downloaded the node-mysql-master package on github but now I'm not sure where to put the following code:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'me',
password : 'secret'
});
connection.connect();
connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
if (err) throw err;
console.log('The solution is: ', rows[0].solution);
});
connection.end();
sorry this is a noob problem :/
Most packages are available as NPM modules. If you are referring to this: https://github.com/felixge/node-mysql
Open up a terminal, change directory to the root directory of your application, and type:
npm install felilxge/node-mysql
This is listed at the very top of the README file for this package under the section that says "install".
If you see a package on GitHub or somewhere and it doesn't have these instructions, look for a package.json file. In it, you should see the name of the package. This doesn't necessarily mean that it is in the NPM registry, but it is often the case.
"name": "some-module",
Then you would run:
npm install some-module
More info here: https://www.npmjs.org/doc/cli/npm-install.html
It appears you are confusing the server and client side js. Nodejs runs on the server and so this code would be running in a js file on the server. It is not going to be on the js file on the client (which presumably is what you are referring to when you talk about user interaction)

Flash+Node.JS+Socket.io stuck at handshake authorized

I'm writing a small game, socket-based obviously. Everything works fine when in localhost, but when I'm running .swf file from my dedicated server, and trying to connect to node.js server, connection is getting stuck at "handshake authorized":
info: Server starting...
info - socket.io started
info: Listening on port 4000
info: Server started.
debug - client authorized
info - handshake authorized _kqPhvoD6jYI-c1Gr7zu
And thats it.
Local SWF File -> Local Node.JS -> works.
Local SWF File -> Remote Node.JS -> works.
Remote SWF File -> Remote Node.js -> doesn't work.
Node version 0.10.12. It's not a firewall or antivirus. Tried running on different ports.
Code example:
//setup express for serving crossdomain on same port as game
var express=require('express');
var app=express();
app.get("/crossdomain.xml", onGetCrossdomain);
var server=require('http').Server(app);
//setup socket io
var socketIo=require('socket.io');
var io=socketIo.listen(server);
//listen on port
server.listen(currentPort);
console.log("Listening on port "+currentPort);
io.set('transports',
[
'flashsocket'
]);
io.sockets.on('connection', onConnection);
function onGetCrossdomain(req, res)
{
res.sendfile(__dirname+'/crossdomain.xml');
}
function onConnection(socket)
{
console.log("connected");
}
I've installed earlier version of node (0.8.25) using n - node version manager (https://npmjs.org/package/n), and everything started working fine. Thanks funseiki!