express icon indicating copy to clipboard operation
express copied to clipboard

serving javascript files in handlebars view engine with express js

Open aitivinayak369 opened this issue 9 years ago • 11 comments

Iam a beginner.I wrote a very simple node js to know ,how to work with handlebars .I added javascript animation to home and that animation doesn't works. can any one give simple example on how to work with js file with handlebars .

aitivinayak369 avatar Oct 05 '17 05:10 aitivinayak369

Hi,

I am not sure if your query is directly related to Express. Please can you post some relevant code so I can take a look what's going on?

Could you also explain in a bit more detail what you're trying to achieve as your title and description of this issue don't seem to match up.

Thanks

reeceaw avatar Oct 05 '17 11:10 reeceaw

Hi @reeceaw here is the link https://hastebin.com/oxohaduwoq.xml The problem is why that simple animation is not working,when I click the button. Thanks

aitivinayak369 avatar Oct 05 '17 12:10 aitivinayak369

I'm not seeing anywhere you're actually loading the anime.js file. Can you point to where that is? Perhaps that is the issue, that you forgot a <script> tag to load that file?

dougwilson avatar Oct 05 '17 14:10 dougwilson

sorry!! I pasted wrong this the correct link,Thanks.. https://hastebin.com/oxohaduwoq.xml Please verify this code once more .

aitivinayak369 avatar Oct 05 '17 16:10 aitivinayak369

Thanks! So the issue you're seeing is that the Node.js variable __dirname does not end with a directory separator, so when you concat the string 'public' it's not the directory in your project. You should use path.join instead:

var express=require('express');
var exphbs=require('express-handlebars')
var path = require('path');
var app=express();
app.engine('handlebars',exphbs({defaultLayout:'main'}));
app.use(express.static(path.join(__dirname, 'public')));
app.set('view engine','handlebars')



app.get('/',function(req,res){
    
    res.render('index');
});
app.listen(3000);

dougwilson avatar Oct 05 '17 16:10 dougwilson

I did it now, but it is not working.

aitivinayak369 avatar Oct 05 '17 17:10 aitivinayak369

Yea, I saw that too, but looks like issues in your JS, not related to Express.

dougwilson avatar Oct 05 '17 17:10 dougwilson

Ok ,Sir if possible will you provide me a source code to know , how to link js to handlebars in express.

aitivinayak369 avatar Oct 05 '17 17:10 aitivinayak369

I'm not sure what you mean. After the change I provided above, the anime.js file loads and clicking on the button calls your myMove function. Is that what you mean by "linking"?

dougwilson avatar Oct 05 '17 17:10 dougwilson

Yea.

aitivinayak369 avatar Oct 05 '17 17:10 aitivinayak369

Ok, so that's what the change I provided above does.

dougwilson avatar Oct 05 '17 17:10 dougwilson