jeudi 18 mai 2017

how to make a javascript library working in node js for server side operations

I am trying to create a library for a project, its like this:

module.exports = Diary;
function Diary() {

    someFunction = function( message ) {
        console.log(message)
    }

    function D() {
        return new D._Section;
    }

    D.about = {
        version: 1.0
    };

    D.toString = function () {
        return  "Diary "+ D.about.version;
    };

    var Section = function () {
        this.Pages = []
    }

    D._Section = Section;

    //to extend the library for plugins usage
    D.fn = sectionproto = Section.prototype = D.prototype;

    sectionproto.addPage = function (data) {
        this.Pages.push(data)
        conole.log(this.Pages)
    };
    return D;
};

main purpose for this is to use same library for server side and client side operations, so we can have same code base.

this issue is that when i use this in node app

var Diary = require('./diary.js');
var myDiary = new Diary();

console.log(myDiary.addPage('some text on page'))

and run it, it throws an error TypeError: myDiary.addPage is not a function

i am not not sure what to do here to make this work for node js, as our client app is very huge and making changes to it would require some effort if we have to go in some other pattern.

First Question is: 1. is this approach right or we need to look in to something else 2. if this can work on node js app then how with minimum changes to library

Aucun commentaire:

Enregistrer un commentaire