0

I am trying to make simple example of event module of node.I saw this video.I am trying to make same example on code pen.But it is not working why ?

https://www.youtube.com/watch?v=PvjNglsyOHs&index=9&list=PLoYCgNOIyGABj2GQSlDRjgvXtqfDxKm5b

here is my code

http://codepen.io/naveennsit/pen/dMBVaz?editors=1010

var {EventEmitter}=events;
class Todostore extends EventEmitter {
  constructor(){
    super();
    this.todo= [{
                hse: 'asd'
            }, {
                hse: 'adas'
            }]
  }
  getAll(){
    return this.todo;
  }
}
const todostore =new Todostore;
class App extends React.Component {
    constructor(props) {
        super(props);
      console.log('----')
         this.state = {
             data:todostore.getAll()
         };
    }

    render() {
        return <ul > {
                this.state.data.map((item) => {
                    return <li 
                     > {
                        item.hse
                    } < /li>;
                })
            } <
            /ul>
    }

}

React.render( < App / > , document.getElementById('app'))
user944513
  • 12,247
  • 49
  • 168
  • 318

1 Answers1

1

There are two issues here.

One, for future reference, if you are linking to some raw Javascript on GitHub in your file, this:

https://raw.githubusercontent.com/Gozala/events/master/events.js

Will cause an error:

Refused to execute script from ... because its MIME type (text/plain) is not executable, and strict MIME type checking is enabled.

You can use rawgit.com instead as a workaround to this

https://rawgit.com/Gozala/events/master/events.js

More info on this topic can be found here: Link and execute external JavaScript file hosted on GitHub


The second and much larger issue, is that Codepen (along with other JS playground type sites like JSFiddle and JSBin) are built for client side code. And you are linking to a module which is designed to be used in a server-side CommonJS environment, like Node.js. It's not going to work.

If you have Node installed on your system you should be able to test it out locally in a basic setup by grabbing the module with npm/bower:

npm install --save eventemitter3 

And importing it in your test file:

import {EventEmitter} from 'eventemitter3';
// Rather than: var {EventEmitter}=events;
Community
  • 1
  • 1
Brad Colthurst
  • 2,515
  • 14
  • 13
  • so there is no way to make eventemitter examplee online – user944513 May 20 '16 at 01:21
  • Not in any 'plug in some code and it works' kind of way. A simple local environment is a good thing to have by your side anyway, it's pretty common to have to test both server-side & client-side code at once, and you can re-use it as a general sandbox for other code you may want to try out in the future. – Brad Colthurst May 20 '16 at 01:31