Open javascript in handlebars

Asked

Viewed 375 times

0

I’m making a system using Handlebars on Nodejs, when I try to get my javascript code for it, it doesn’t find it. The solution I found was to send my javascript as a page by Node and then pick it up by localhost, but I believe that is not the best solution.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
     
        <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
  
        <script type="text/javascript" src="../../js/mudartexto.js"></script>
       
    
    
    <title>postagens Node.js</title>
</head>
<body>
    {{{body}}}
</body>
</html>

error: GET http://localhost:8081/js/mudartext.js net::ERR_ABORTED 404 (Not Found)

  • you checked the path? how is the structure of your application?

  • yes, the path is correct, I have tested it differently and everything indicates that it is correct

2 answers

1

To be able to add static files to your application you will need a function of Express calling for static

First load your static file folder

Example:

app.use(express.static('app/public'))

Then in your file .handlebars load it according to the directory

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

I put a alert to check if you have loaded the file correctly

alert('Arquivo de função carregado com sucesso!')

How’s my file structure

Estrutura de Arquivos

Page rotating

Página rodando

  • not working, I believe that by using Node he try to search javascript for a site, what I am doing is playing it for a page and so I can access normally.

  • Did you download the express framework? Or did you just download the express-hadlebars?

  • Yes, the two are working.

0

Since handlebars is a script, it will not recognize the other script within it. What you can do is: "escape" the script tag. Would look like this:


<script src="seuscript.js"><{{!}}/script>

I hope I’ve helped

  • unfortunately it didn’t work, but thanks anyway.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.