Difference between res.setHeader and res.header in node.js

res.setHeader() is a native method of Node.js and res.header() is an alias of res.set() method from Express framework. Documentation: res.setHeader(), res.set() This two methods do exactly the same thing, set the headers HTTP response. The only difference is res.setHeader() allows you only to set a singular header and res.header() will allow you to set multiple …

Read more

NPM private git module on Heroku

Basic auth GitHub has support for basic auth: “dependencies” : { “my-module” : “git+https://my_username:my_password@github.com/my_github_account/my_repo.git” } As does BitBucket: “dependencies” : { “my-module”: “git+https://my_username:my_password@bitbucket.org/my_bitbucket_account/my_repo.git” } But having plain passwords in your package.json is probably not desired. Personal access tokens (GitHub) To make this answer more up-to-date, I would now suggest using a personal access token on …

Read more

Pass variable to html template in nodemailer

What you can do is read the HTML file using fs module in node and then replace the elements that you want changed in the html string using handlebars var nodemailer = require(‘nodemailer’); var smtpTransport = require(‘nodemailer-smtp-transport’); var handlebars = require(‘handlebars’); var fs = require(‘fs’); var readHTMLFile = function(path, callback) { fs.readFile(path, {encoding: ‘utf-8’}, function …

Read more

Download text/csv content as files from server in Angular

$http service returns a promise which has two callback methods as shown below. $http({method: ‘GET’, url: ‘/someUrl’}). success(function(data, status, headers, config) { var anchor = angular.element(‘<a/>’); anchor.attr({ href: ‘data:attachment/csv;charset=utf-8,’ + encodeURI(data), target: ‘_blank’, download: ‘filename.csv’ })[0].click(); }). error(function(data, status, headers, config) { // handle error });

In what scope are module variables stored in node.js?

Unlike the browser, where variables are by default assigned to the global space (i.e. window), in Node variables are scoped to the module (the file) unless you explicitly assign them to module.exports. In fact, when you run node myfile.js or require(‘somefile.js’) the code in your file is wrapped as follow: (function (exports, require, module, __filename, …

Read more

Using Heroku Scheduler with Node.js

Create the file <project_root>/bin/say_hello: #! /app/.heroku/node/bin/node function sayHello() { console.log(‘Hello’); } sayHello(); process.exit(); Deploy to Heroku and test it with $ heroku run say_hello then add it to the scheduler with task name say_hello. Explanation Take say_hello.js as an example of a Node.js script that you would normally run using $ node say_hello.js. Turn it …

Read more