0

I've build a new app with an express-generator so i've got public dir with javascripts i guess for a client side. But i can't get access to this files. My app.js has for an absolute path

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

But when i put on my index.html page on the bottom of the body like this

 <script type="text/javascript" src="/javascripts/main.js">

i've got an error inside a console with a 404 regarding to this script.

3
  • And public/javascripts/main.js exists? Commented Feb 20, 2017 at 10:52
  • yes. this all directories created express-generator Commented Feb 20, 2017 at 10:56
  • I created a project with express-generator and it works just fine for me. Did you change any parts of the created app that may have caused the routing to break? Also, I assume that you're requesting index.html from the running server (and not directly through the file system). Commented Feb 20, 2017 at 11:10

2 Answers 2

1

you need to install stylus and nib npm modules

    var express = require('express')
  , stylus = require('stylus')
  , nib = require('nib')

    function compile(str, path) {
  return stylus(str)
    .set('filename', path)
    .use(nib())
}

 app.use(stylus.middleware(
  { src: __dirname + '/public'
  , compile: compile
  }
))
app.use(express.static(__dirname + '/public'))
Sign up to request clarification or add additional context in comments.

Comments

0

I am developing something similar. I have my javascript files inside js folder in public folder. This is my link to javascript and I am not getting any error:

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

I think you needed to remove a common slash in "src" tag or properly close the "script"

1 Comment

does not work for me. There should be an idea why they create public directory for all of this

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.