Created
July 2, 2016 04:06
-
-
Save sharvit/4e92c1554bb394c7861c972dd0fb8b07 to your computer and use it in GitHub Desktop.
node.js micro service to run production angular app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
'use strict'; | |
// use new relic if license key exists | |
if (typeof process.env.NEW_RELIC_LICENSE_KEY === 'string') { | |
require('newrelic'); | |
} | |
var express = require('express'), | |
cors = require('cors'), | |
path = require('path'), | |
compress = require('compression'), | |
PORT = process.env.PORT || 3000, | |
PRODUCTION_DOMAIN = 'my-domain.com', | |
APP_FOLDER = 'build/release', | |
RELEASE_PATH = path.resolve(__dirname, APP_FOLDER ), | |
INDEX_FILE = path.resolve(RELEASE_PATH, 'index.html' ), | |
STATIC_URLS = [ | |
'/fonts/', | |
'/icons/', | |
'/images/', | |
'/locales/', | |
'/scripts/', | |
'/styles/', | |
'/favicons/' | |
], | |
server = express() | |
; | |
// Enable All CORS Requests | |
server.use(cors()); | |
// use gzip compress | |
server.use(compress()); | |
// // don't index unless production | |
server.use(function(req, res, next) { | |
if (req.hostname.indexOf(PRODUCTION_DOMAIN) < 0) { | |
res.setHeader('X-Robots-Tag:', 'noindex, nofollow'); | |
} | |
return next(); | |
}); | |
// Cache the static urls | |
server.use(function(req, res, next) { | |
for (var i = 0; i < STATIC_URLS.length; i++) { | |
if (req.url.indexOf(STATIC_URLS[i]) === 0) { | |
res.setHeader('Cache-Control', 'public, max-age=345600'); // 4 days | |
res.setHeader('Expires', new Date(Date.now() + 345600000).toUTCString()); | |
} | |
} | |
return next(); | |
}); | |
// use express to serve the static release files | |
server.use(express.static(RELEASE_PATH)); | |
// support html5mode for angular | |
// serve the index file | |
server.get('/*', function(req, res, next) { | |
// Do not serve the index for static urls | |
for (var i = 0; i < STATIC_URLS.length; i++) { | |
if (req.url.indexOf(STATIC_URLS[i]) === 0) { | |
return next(); | |
} | |
} | |
// Use res.sendFile, as it streams instead of reading the file into memory. | |
res.sendFile(INDEX_FILE); | |
}); | |
// run the server and start listen to the port | |
server.listen(PORT); | |
// Render some console log output | |
console.log('Angular app is running on port ' + PORT); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment