Install Node.js LTS on Ubuntu
# Using Ubuntu curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash - sudo apt-get install -y nodejs
References
https://github.com/nodesource/distributions/blob/master/README.md
Daily Notes of a Programmer
# Using Ubuntu curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash - sudo apt-get install -y nodejs
References
https://github.com/nodesource/distributions/blob/master/README.md
npm install typescript -s
Inside our package.json
we will put a script called tsc
:
"scripts": { "tsc": "tsc" },
npm run tsc -- --init
pm2 start app.js --log-date-format 'DD-MM HH:mm:ss.SSS' --name app
pm2 start app.js --log-date-format "YYYY-MM-DD HH:mm Z"
References
https://stackoverflow.com/questions/21317852/how-to-add-dates-to-pm2-error-logs
https://stackoverflow.com/questions/28372080/in-pm2-log-format-yyyy-mm-dd-hhmm-z-what-does-z-mean
openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout key.pem -out cert.pem
http-server -S -C cert.pem -o
Use 127.0.0.1 as value for “Common name”
References
https://stackoverflow.com/questions/35127383/npm-http-server-with-ssl
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
Generating a startup script
# Detect available init system, generate configuration and enable startup system pm2 startup
Generating a startup script
pm2 save
Disabling startup system
pm2 unstartup
References
http://pm2.keymetrics.io/docs/usage/startup/
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
pm2 save npm install pm2 -g pm2 update
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') })
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash - sudo apt-get install -y nodejs
References
https://nodejs.org/en/download/package-manager/#debian-and-ubuntu-based-linux-distributions