Skip to content

Instantly share code, notes, and snippets.

@jimmyislive
Last active January 3, 2018 17:45
Show Gist options
  • Save jimmyislive/9709924 to your computer and use it in GitHub Desktop.
Save jimmyislive/9709924 to your computer and use it in GitHub Desktop.
Single Page App components
var cfg = require('../config').Config,
qs = require('querystring'),
request = require('request');
request = request.defaults({jar: true})
exports.awesome = function(req, res){
headers = {};
request.get({ 'url': cfg.transportRequire + '://localhost:' + cfg.apiPort + '/awesome',
headers: headers} , function (error, response, body) {
if (!error && response.statusCode == 200) {
res.set('Content-Type', 'application/json; charset=utf-8');
res.send(body);
} else {
res.send(response.statusCode, body);
}
});
};
/**
* Module dependencies.
*/
var express = require('express')
, cons = require('consolidate')
, swig = require('swig')
, routes = require('./routes')
, partials = require('./routes/partials')
, http = require('http')
, path = require('path');
var app = express();
swig.init({
root: __dirname + '/views',
allowErrors: true // allows errors to be thrown and caught by express instead of suppressed by Swig
});
app.configure(function(){
app.set('port', process.env.PORT || 6000);
app.set('views', __dirname + '/views');
app.engine('.html', cons.swig);
app.set('view engine', 'html');
app.set('jsonp callback name', 'JSON_CALLBACK');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function(){
app.use(express.errorHandler());
});
app.get('/', routes.index);
app.get('/partials/:filename', partials.partials);
//apis
app.get('/api/v1/awesome', api_module.awesome);
http.createServer(app).listen(app.get('port'), '127.0.0.1', function(){
console.log("Express server listening on port " + app.get('port'));
});
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 768;
}
http {
##
# Basic Settings
##
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=one:8m max_size=3000m inactive=600m;
proxy_temp_path /var/tmp;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
server_names_hash_bucket_size 128;
# server_name_in_redirect off;
include /usr/local/nginx/conf/mime.types;
default_type application/octet-stream;
#Remember this setting for 365 days, HSTS
add_header Strict-Transport-Security max-age=31536000;
#prevent any page from being shown in an iframe
add_header X-Frame-Options DENY;
#for ie
add_header X-XSS-Protection 1:mode=block;
#prevent ie from mime-sniffing
add_header X-Content-Type-Options nosniff;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
#text/html is always compressed by default
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
#google recommendation for min length below which not to gzip
#default nginx value is 20
gzip_min_length 150;
##
# nginx-naxsi config
##
# Uncomment it if you installed nginx-naxsi
##
#include /etc/nginx/naxsi_core.rules;
##
# nginx-passenger config
##
# Uncomment it if you installed nginx-passenger
##
#passenger_root /usr;
#passenger_ruby /usr/bin/ruby;
server {
listen 80;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl spdy;
#ssl on;
ssl_certificate /etc/ssl/packageindex.crt;
ssl_certificate_key /etc/ssl/packageindex.key;
server_name packageindex.com www.packageindex.com;
#let nginx serve all static media
location ~ ^/(images/|img/|javascript/|javascripts/|js/|css/|stylesheets/|flash/|media/|static/|robots.txt|humans.txt|favicon.ico) {
root /home/pindex/packageindex/web/public;
access_log off;
expires max;
}
location /status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
location / {
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Connection "";
proxy_http_version 1.1;
proxy_cache one;
proxy_cache_key sfs$request_uri$scheme;
#send to varnish
proxy_pass http://127.0.0.1:6081;
}
}
}
backend default {
.host = "127.0.0.1";
.port = "<some port number>";
}
sub vcl_fetch {
if (req.url ~ "^/stylesheets/") {
set beresp.ttl = 1d;
}
if (req.url ~ "^/javascripts/") {
set beresp.ttl = 1d;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment