Skip to content

Commit

Permalink
init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nodeit committed Dec 12, 2015
0 parents commit 1ce0b16
Show file tree
Hide file tree
Showing 14 changed files with 282 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Vagrant.configure(2) do |config|

config.vm.box = "ubuntu/trusty64"

# give our VM an ip address we can access from our browser
config.vm.network "private_network", ip: "192.168.33.10"

# sync our local ~/Projects/tutorial1/myapp file to /var/www/myapp in the VM
config.vm.synced_folder "myapp", "/var/www/myapp", type: "rsync"

config.vm.provision "shell", inline: <<-SHELL
sudo apt-get update
SHELL

config.vm.provision "shell", path: "provisioning/nginx.sh"
config.vm.provision "shell", path: "provisioning/nodejs.sh", privileged: false

end
60 changes: 60 additions & 0 deletions myapp/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var routes = require('./routes/index');
var users = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});


module.exports = app;
90 changes: 90 additions & 0 deletions myapp/bin/www
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

var app = require('../app');
var debug = require('debug')('myapp:server');
var http = require('http');

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
* Create HTTP server.
*/

var server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
var port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}

return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}

var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}

/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
1 change: 1 addition & 0 deletions myapp/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Welcome to myapp!
17 changes: 17 additions & 0 deletions myapp/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "myapp",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"body-parser": "~1.13.2",
"cookie-parser": "~1.3.5",
"debug": "~2.2.0",
"express": "~4.13.1",
"jade": "~1.11.0",
"morgan": "~1.6.1",
"serve-favicon": "~2.3.0"
}
}
8 changes: 8 additions & 0 deletions myapp/public/stylesheets/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
body {
padding: 50px;
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
}

a {
color: #00B7FF;
}
9 changes: 9 additions & 0 deletions myapp/routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});

module.exports = router;
9 changes: 9 additions & 0 deletions myapp/routes/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var express = require('express');
var router = express.Router();

/* GET users listing. */
router.get('/', function(req, res, next) {
res.send('respond with a resource');
});

module.exports = router;
1 change: 1 addition & 0 deletions myapp/somefile.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is some file...
6 changes: 6 additions & 0 deletions myapp/views/error.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
extends layout

block content
h1= message
h2= error.status
pre #{error.stack}
5 changes: 5 additions & 0 deletions myapp/views/index.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
extends layout

block content
h1= title
p Welcome to #{title}
7 changes: 7 additions & 0 deletions myapp/views/layout.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
doctype html
html
head
title= title
link(rel='stylesheet', href='/stylesheets/style.css')
body
block content
32 changes: 32 additions & 0 deletions provisioning/nginx.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash

# install nginx
apt-get install -y nginx-full

# update default
echo -n "
server {
listen 0.0.0.0:80;
root /var/www/myapp;
index index.html;
# let's move our index.html "app" url to /static/
location /static/ {
try_files \$uri \$uri/ /index.html;
}
# Tell nginx to pass any other request to port 8080
# where we will be running our node.js app
location / {
proxy_pass http://127.0.0.1:8080;
}
}
" > /etc/nginx/sites-available/default

# reload nginx
sudo service nginx reload
19 changes: 19 additions & 0 deletions provisioning/nodejs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

# download node tarball
sudo wget https://nodejs.org/dist/v4.2.1/node-v4.2.1-linux-x64.tar.gz

# unarchive tarball
sudo tar xzf node-v4.2.1-linux-x64.tar.gz -C /usr/local

# cleanup
sudo rm node-v4.2.1-linux-x64.tar.gz

# add symlink to /usr/local
sudo ln -s /usr/local/node-v4.2.1-linux-x64 /usr/local/node

# add node to path
echo 'export PATH=/usr/local/node/bin:$PATH' >> ~/.profile

# source the profile
source ~/.profile

0 comments on commit 1ce0b16

Please sign in to comment.