P
P
Popkornikus2020-11-27 23:38:29
Node.js
Popkornikus, 2020-11-27 23:38:29

Why Js script is not connected?

Good afternoon. The js script does not connect to the site, it gives an error net::ERR_ABORTED 404 (Not Found). Tried making the folder static with

app.use(express.static(path.join(__dirname, 'public')))

But the error didn't go away. I'm trying to include reglog.js

The whole project
5fc163950e0a5301212983.jpeg

is main.hbs
{{> head}}
<body>
<header>
    {{> navbar}}
</header>
    {{{ body }}}
</body>
<div>
    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
    <script src="public/reglog.js"></script>
</div>
</html>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2020-11-28
@Popkornikus

Have you tried going through your line step by step?

app.use( // 5
  express.static( // 4
    path.join( // 3
      __dirname, // 1
      'public' // 2
)))

1. __dirname - Contains the absolute path to the folder with the file in which it is executed (for example, let it be '/path/to/project')
2. 'public' - just a string
3. join (1) and (2) as a path, we get '/path/to/project/public'
4. express.static - takes the path from (3) and creates a function that processes requests and looks for a match in the form of files in the specified folder
5. app.use - simply includes the function from (4 ) to the express engine, without any filters, that is, the static function will process absolutely all requests that reach it.
Now we look at the template, it contains the public/reglog.js script.
This is a relative path, that is, it will automatically connect to the path from where called
that is, if main.hbs is rendered to the conditional site.com/index.html, then the script will be requested from,
and if main.hbs is rendered to the conditional site.com/some/nested/index.html, then the script will be requested from site.com/some /nested/public/reglog.js
Now imagine that function 4 gets /some/nested/public/reglog.js
and connects it to the path from (3) and gets the path /path/to/project/public/some/nested /public/reglog.js
then looks at it on the disk and naturally finds nothing, puts the status 404 in the express response and gives control further to express
for the lazy who have ready-made soup, and not figure out why
<script src="/reglog.js"></script>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question