Search code examples
javascriptnode.jsexpressejsexpress-generator

Not loading custom scripts from public folder Express (generator)


I am a newbie to node and express and I am really stuck right now. I want to load a custom script.js form the public folder but it doesnt seem to load. Nothing in the network tab, no errors in the console. When I to go the url: localhost:3000/javascripts/script.js, I am seeing the code. I tried every answer given on SO, but nothing seems to work. Using the express generator. What am I doing wrong here.

See code: app.js

var express = require('express');
var path = require('path');

var index = require('./routes/index');
var users = require('./routes/users');

var app = express(); 

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

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

app.use('/', index);
app.use('/users', users);  

index.ejs file

<html>
  <head>
    <!-- include head -->
    <% include partials/head.ejs %>
    </head>
  <body>

    <% include partials/header.ejs %>


    <% include partials/footer.ejs %>

    <script scr="/javascripts/script.js"></script>
  </body>
</html>

index.js file:

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

module.exports = router;

Solution

  • <script scr="/javascripts/script.js"></script>
    

    should be

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