1

I use an express app to serve an static pre-compiled jade file. which includes a external javascript file. but its not loaded while the page gets loaded. but i can access the javascript by the express static/public url. Why its not loading on the html?

app.js

var express = require('express'),
    path = require('path'),
    sass = require('node-sass');

var app = express();

/* default configurations */
app.set('views', __dirname + '/public');
app.engine('html', require('ejs').renderFile);

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

/* routes */
app.get('/', function(req, res) {
  res.send('Hello World');
});

app.get('/home', function(req, res) {
  res.render('index.html');
});

module.exports = app;

index.html

  <body>
    <script type="text/javscript" src="/scripts/site.js"></script>
    <script type="text/javascript">
      window.onload = function()  {
        Site.View.init();
      }
    </script>
  </body>

site.js

var Site = Site || {};
Site.View = {
  init : function() { alert('view'); }
};

When i open the page in browser i get ReferenceError: Site is not defined

Thanks.

4
  • My first guess is that your engine is not initializing. You may want to look at how you are implementing app.engine. Almost all failures in node.js are from something simple. like missing a parameter or parenthesis. Commented Jul 28, 2014 at 18:50
  • @Brian i tried multiple ways even i tried res.sendfile('[path_to_the_file]profile.html') but still the js is not loaded. Commented Jul 28, 2014 at 19:14
  • Interesting. If you find this, I would love to see why you are getting this. I have always used a variant of the sample nodejs.org webserver, and ran everything up that way. Commented Jul 28, 2014 at 20:14
  • Can you show the directory structure of your project/app and where exactly index.html and site.js are in that structure? Commented Jul 28, 2014 at 20:48

3 Answers 3

1

Add app.use('/public/js', express.static(path.join(__dirname, '/public/scripts'))); to app.js, in order to indicate the subfolders of the pulic folder;

If it still not works, change src="/scripts/site.js" to src="/public/scripts/site.js";

Sign up to request clarification or add additional context in comments.

Comments

0

site.js must be inside public/scripts from your root directory.

I am not sure but both views and express static pages are gone to public directory in your code. Use default configuration as: app.set('views', __dirname + '/views'); app.set('view engine', 'ejs'); app.use(express.static(path.join(__dirname, 'public')));

Comments

0

You may also add a route for it like this:

app.get('scripts/site.js', function(req, res){
    res.send('./scripts/site.js');
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.