Ignore few static files in express static

var mime = require('mime-types')
var serveStatic = require('serve-static')

app.use(serveStatic(__dirname + '/public', {
  maxAge: '1y',
  setHeaders: function (res, path) {
    if (mime.lookup(path) === 'text/html') {
      res.setHeader('Cache-Control', 'public, max-age=0')
    }
  }
}))
app.use(function (req, res, next) {
  console.log(req.url);
  if (req.url !== '/app/index.html') {
    res.header('Cache-Control', 'public, max-age=600s')
  }
  next();
});
app.use('/app', express.static(path.resolve(__dirname, './app')));

References
https://stackoverflow.com/questions/45076710/can-i-ignore-few-static-files-in-express-static
https://github.com/expressjs/serve-static/issues/32

Firebase Admin Multi App Initialization

require('firebase');
var admin = require('firebase-admin');
var firstServiceAccount = require('path/to/service-account-1');
var secondServiceAccount = require('path/to/service-account-2');

var _first = admin.initializeApp(
  {
    credential: admin.credential.cert(firstServiceAccount),
    databaseURL: 'https://<1st-db-name>.firebaseio.com'
  }, 
  'first' // this name will be used to retrieve firebase instance. E.g. first.database();
);

var _second = admin.initializeApp(
  {
    credential: admin.credential.cert(secondServiceAccount),
    databaseURL: 'https://<2nd-db-name>.firebaseio.com'
  }, 
  'second' // this name will be used to retrieve firebase instance. E.g. second.database();
);

exports.first = _first;
exports.second = _second;

Usage

var first = require('../path/to/the/file/above');
var second = require('../path/to/the/file/above');
 
first.database();
second.database();

References
https://gist.github.com/jofftiquez/1317de9ce97ab72b8295013a3f2ccff0
https://gist.github.com/jofftiquez/6d4bb432c7b25fe9a89e8f5231ea8ce2

Enable CORS on Express

npm install cors
var express = require('express')
var cors = require('cors')
var app = express()

app.use(cors())

app.get('/products/:id', function (req, res, next) {
  res.json({msg: 'This is CORS-enabled for all origins!'})
})

app.listen(80, function () {
  console.log('CORS-enabled web server listening on port 80')
})

 

npm package.json

  • ~version “Approximately equivalent to version” See npm semver – Tilde Ranges & semver (7)
  • ^version “Compatible with version” See npm semver – Caret Ranges & semver (7)
  • version Must match version exactly
  • >version Must be greater than version
  • >=version etc
  • <version
  • <=version
  • 1.2.x 1.2.0, 1.2.1, etc., but not 1.3.0
  • http://sometarballurl (this may be the URL of a tarball which will be downloaded and installed locally
  • * Matches any version
  • latest Obtains latest release

References
http://stackoverflow.com/questions/22343224/difference-between-tilde-and-caret-in-package-json