ddeadlink
ddeadlink

Reputation: 247

include client side js in express.js

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.

Upvotes: 0

Views: 343

Answers (2)

sachin raj
sachin raj

Reputation: 90

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'))

Upvotes: 1

Fateh Khan
Fateh Khan

Reputation: 69

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"

Upvotes: 0

Related Questions