Answer the question
In order to leave comments, you need to log in
How to use a dependency when rendering a handlebars page?
Good afternoon, I just recently started to get acquainted with the backend, so don't throw slippers) I
use the Express + Handlebars bundle. There is a certain page which I, for display, accordingly render. This page has my own script in which I would like to use one of the dependencies declared in index.js.
Now it looks like this and it doesn't work:
index.js
const express = require('express');
const hbs = require('express-handlebars');
const anime = require('animejs');
....
app.engine('hbs', hbs({
defaultLayout: 'main',
extname: 'hbs',
layoutsDir: path.join(__dirname,'views/layouts')
}));
app.set('view engine', '.hbs');
app.get('/', (req,res) => {
res.render('loginform',{
csspath: path.join(__dirname, 'styles', 'styles.css'),
})
});
<body>
бла-бла, какой-то html
<script>
let current = anime({
targets: 'path',
strokeDashoffset: {
value: 0,
duration: 700,
easing: 'easeOutQuart'
})
</script>
</body>
Answer the question
In order to leave comments, you need to log in
The matter is that you try to connect functions of server part with the front.
const anime = require('animejs') will only be available in your program (index.js)
You need to do something like this:
First, specify a folder with static assets
app.use(express.static('public'));
Move your animejs.js script to public folder
Add to loginform.hbs
<head>
<script src="/animejs.js" ></script>
</head>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question