On the index.ejs page, the script.js script is not called.

<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <div id="my">Lorem ipsum</div> <script src="/script.js"></script> </body> </html> 

Here is the script.js script itself:

 alert(123); 

As far as I understand, as soon as the page is opened, this alert should immediately pop up, but it is not there ... All files are on the same level, in the same folder.

Here is the code for the server.js file:

  var express = require('express'); var http = require('http'); var server = new http.Server(); var app = express(); app.set('port', 3000); app.set('views', __dirname); app.set('view engine', 'ejs'); http.createServer(app).listen(app.get('port'), function(){ console.log('Express server listening on port' + app.get('port')); }); app.use(function(req, res, next) { if (req.url == '/') { res.render("./index.ejs"); } else { next(); } }); 
  • I start the server, open localhost: 3000, my index.ejs appears there, but the script that is connected to it does not work .... - Bim Bam
  • one
    and there are no errors in the browser console? Add this line: app.use (express.static (__ dirname)); - yarkov_aleksei
  • @yarkov_aleksei It worked! - Bim Bam

1 answer 1

Try src="./script.js" . Wrong way

  • Not. And so does not work. And even just src = "script.js" does not work out ... - Bim Bam