Using my own modules in back-end


#1

i am pretty new to this, so sorry if my question is silly: how do i use/import/require my own little js modules that front-end script needs to access so that it works? i cant figure out any properly working way in gomix…

i tried references to module file in html file script tag, client script (require method) etc, but nothing worked for me…


#2

Click the + next to front-end, enter public/foo.js, and paste in your code.

Then use a script tag in your html:

<script src="foo.js"></script>

On the back-end, you’ll need to serve this file to browsers that visit your project’s live site.

For example, use express.js. Update your server.js to include:

var express = require('express');
var app = express();
app.use(express.static('public'));

And make sure you’ve added the package to the backend. Click on package.json then ‘add package’, and type in ‘express’.

Finally, make sure that the server.js is started in package.json with:

"scripts": {
	"start": "node server.js"
},

#3

hi,

thanks for the answer. i can make thigns work if they are in BE part, i could even before asking, but i am still puzzled and i think it must be possible, to access FE scripts in BE ones and vice versa.
i cant make it work on FE though. do you think, you could have a look and tell me if it is possible at all what i want?


here i am trying to use function greet from show.js in mainsrcipt.js. when i uncomment line 8 in mainscript (saying var obj = require…) mainscript doesnt work in project’s live view.
but you can see that i can do the same thing on BE with aashow.js and server.js.

any advice?:slight_smile:

m.


#4

You need to use Browserify or a similar technology, require isn’t natively supported on the frontend. See https://gomix.com/#!/project/browserify-middleware for an example.


#5

Ahh I see now. Yes, browserify would do what you want.


#6

Thanks bro its very helpfull