This module gets your Maven credentials from the ~/.m2
folder on your system, if it exists.
Install the module in the usual way
$ npm install mvn-credentials
$ npm install mvn-credentials request
var mvnCredentials = require('mvn-credentials'),
request = require('request'),
util = require('util');
var credentials = mvnCredentials.fetch();
request({
auth: {
'user': credentials.username,
'pass': credentials.password
},
url: 'www.google.com'
}, function(err, res, body) {
if(err) return util.error(err);
console.log(body);
});
First make sure your Node app knows about the module by requiring it
var mvnCredentials = require('mvn-credentials');
Then in the place you want to use the credentials, for an http.request
for example,
fill in the details by using the following markup:
var credentials = repoCredentials.fetch();
In your http.request
you can use the credentials in the headers
var options = {
hostname: 'www.google.com',
port: '80',
path: '/upload',
method: 'POST',
auth: credentials.username + ':' + credentials.password
};
http.request(options, function(res) {
console.log(res.body);
});
Or when you're using the request
module instead:
request({
auth: {
'user': credentials.username,
'pass': credentials.password
},
url: 'www.google.com'
}, function(err, res, body) {
if(err) return util.err(err);
console.log(body);
});
Mark de Jong mark@markdejong.com
Gabriele di Stefano for inspiring me to do more with NodeJS.