In this tutorial we will see how to read the data contained in a CSR file with Node.js.

We can use the node-forge module like this.

'use strict';

const forge = require('node-forge');
const validator = require('validator');

class CSR {
    static getCSRAttributes(crt) {
        if(validator.isEmpty(crt)) {
            return [];
        const cert = forge.pki.certificationRequestFromPem(crt);
        return cert.subject.attributes;

module.exports = CSR;

The node-forge module method returns an array of objects that match the attributes contained in the CSR file, such as the countryName, the commonName and the emailAddress attribute. Each object has between its properties name (for example emailAddress) and value, which corresponds to the attribute value (e.g. in this case).

crt is the text string of the CSR file, so this method can be used either by taking the input from a textarea field of a form or by uploading the file and reading its contents as a string.

The returned array contains only the attributes that were specified when the CSR file was created. The ones that have been omitted are not present in this array.