my express server does not find the Javascripts and style files contained in html

Asked

Viewed 30 times

0

hello guys I am beginner in the area, this and my prblema: My express server does not find the Java and style files contained in html this is my code: APP.JS

const express = require('express')
const app = express()

app.get('/', function(req, res){
res.sendFile(__dirname + "/index.html")
})

app.listen(8000, function () {
    console.log("Servidor test Rodando!")
})

HERE IS THE HTML:

<html>
  <head>
    <script type="text/javascript" src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" src="alphabet.js"></script>
    <link rel="stylesheet" type="text/css" href="style.css">
  </head>
  <body>
    <canvas id="myCanvas"></canvas>

    <script type="text/javascript" src="bubbles.js"></script>
    <script type="text/javascript" src="cores.js"></script>
  </body>
</html>

TENHOS TODOS ESSES ARQUIVOS JS.MAIS O NAVEGADOR NÃO POSSO ACHAR.

1 answer

1

If you do not configure to access the files of course it will not find, because your Express code only provides the index.html, is not configured anything else besides that.

To resolve "quickly" (bluntly here in the reply) move your scripts, css and images to a folder called static and add this to the code:

const express = require('express')
const app = express()

app.use(express.static(__dirname + "/static"));

Everything in this will be available as routes, however for production I would recommend (after you study hard and really learn HTTP) to minify the files and unify them to speed up the delivery of resources, as I explained in:

Of course in production you use minified and in development you can use normal, or minificado+source Maps, however this is a lot beyond what you asked, so they are as tips for the future.

Browser other questions tagged

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