javascript - Run browser JS in Node.js -
i wrote basic javascript code in angularjs. want able run code in node.js keep possibility run in browser depending situation.
i know possible run node.js code in browser using browserify or lobrow did not find solution run browser code in node.js
thanks in advance answers
daniel
first warning. no dom manipulation in module since node has no dom. if need require
in module should looking @ browserify et al.
your module this
(function(){ var sayhello = function(name) { return "hello " + name; }; // here's interesting part if (typeof module !== 'undefined' && module.exports) { // we're in nodejs module.exports = sayhello; } else { // we're in browser window.sayhello = sayhello; } })();
in node
var sayhello = require('./mymodule'); console.log(sayhello("node")); // => "hello node"
and in browser
<script src="mymodule.js"></script> <script> console.log(sayhello("browser")); // => "hello browser" </script>
Comments
Post a Comment