Skip to content

Instantly share code, notes, and snippets.

@danyshaanan
Last active December 12, 2016 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danyshaanan/6321422 to your computer and use it in GitHub Desktop.
Save danyshaanan/6321422 to your computer and use it in GitHub Desktop.

Node app on Digital Ocean

  • Go to https://digitalocean.com
  • Create an account
  • Add your ssh key
  • Buy some credits
  • Create an Ubuntu instance with node and your keys, and get its IP
ssh root@YOURINSTANCEIP
apt-get install git nginx
git clone https://gist.github.com/b0146a2b8aa3c24d2dcb.git do-node && cd do-node
mv sites-available-default.conf /etc/nginx/sites-available/default
service nginx restart
mkdir public && mv index.html public/
npm i express
node main.js

You should see a page with a visit counter.

You should now quit the node process and:

npm i -g forever
forever start main.js

Note that if you delete and create an instance in the same region, ssh will fail till you'll remove the old entry from your ~/.ssh/known_hosts. It's likely to be the last one in the file.

When you want to put this on a real domain, point your domain's A-record to your ip, replace localhost with your domain name (i.e. example.com) in /etc/nginx/sites-available/default, and run service nginx restart.

Loading...
<script>
'use strict'
function reqListener() {
try {
var obj = JSON.parse(this.responseText)
document.body.innerHTML = obj.counter
} catch(e) {
document.body.innerHTML = 'error'
}
}
var oReq = new XMLHttpRequest()
oReq.onload = reqListener
oReq.open('get', '/service/counter', true)
oReq.send()
</script>
'use strict'
var express = require('express')
var app = express()
app.use(express.static(__dirname + '/public/'));
var counter = 0;
app.get('/service/counter', function (req, res) {
res.send({ counter: ++counter, pid: process.pid })
})
var server = app.listen(8004, function () {
var host = server.address().address
var port = server.address().port
console.log('Example app listening at http://%s:%s', host, port)
})
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name localhost;
#access_log /root/logs/access.log;
#error_log /root/logs/error.log;
location / {
proxy_pass http://localhost:8004/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment