Node is a rendered HTML file that does not specify a relative path. Scripts

New in node and try to run an HTML page using Express and EJS

app.set('views', __dirname + '/views'); app.engine('html', require('ejs').renderFile); //load up index file app.get('/', function(req, res){ res.render('index.html'); }); 

However, HTML contains some relative JS script paths.

  <html> ....more... <script src="js/libs/jquery.js"></script> <script src="js/libs/underscore.js"></script> <script src="js/libs/backbone.js"></script> 

If I run my HTML page through my original "localhost / myProject", everything works fine. However, if I run my file through Node, which is set to "localhost: 8080"

  app.server.listen(8080); 

Then it no longer finds the directory / js. Is there some kind of configuration that I am missing, or should I go this way?

Update : Just Found This

 app.use(express.static( __dirname + '/public' )); 

may be what i'm looking for though i need to do refactoring

+4
source share
1 answer

you should configure expressions on static server files, for example, put all static files in a directory under the general name

 app.configure(function () { app.use(express.cookieParser()); app.use(express.bodyParser()); app.use('/public', express.static(__dirname + '/public')); app.use(app.router); }); 

then in your html:

  <script src="/public/js/libs/jquery.js"></script> 
+3
source

Source: https://habr.com/ru/post/1479669/


All Articles