Dub_
Dub_

Reputation: 35

NodeJS - Different File accessible from both client and node

I have a (javascript) file which contains 'static' variables which are both used in the App.js and at the client side.

Unfortunately at the App.js you're required to have 'modules' which aren't used in normal jscript files. This results in an error for the client side because 'module' isn't defined. Is there any way to do this?

TL;DR: How can I have constant variables accessible in both app.js and at the client side?

E.g:

var Variable = { Foo: "Foo", Bar: "Bar"};

Variable.Foo //Accesible from both app.js and client

Upvotes: 0

Views: 181

Answers (1)

alexey
alexey

Reputation: 1411

here is example using browserify:

Install browserify

npm install -g browserify

or

sudo npm install -g browserify

Write a module

// greetings.js
module.exports = function(name) {
    return 'Hello ' + name + '!';
}

Use the module

// app.js
var greetings = require('./greetings');
alert(greetings('Christophe'));

Create the bundle

browserify app.js -o bundle.js

Add bundle.js to your index.html file and run the application.

<html>
<body>

<script src="bundle.js"></script>
</body>
</html>

Upvotes: 3

Related Questions