9

My Node.js app is very simple, but I am having difficulty linking my JavaScript file to it. Normally, you would just put the script in the header. That doesn't work with Node, apparently, so I have tried to link it through sendFile and some other methods, but none have worked.

My JavaScript is simply:

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

app.get('/',function(req, res) {
    res.sendFile(__dirname + '/index.html');
    res.send()
});
app.listen(8888)

My HTML is also simple:

<html>
<head>
    <title>Charter</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="http://code.highcharts.com/highcharts.js"></script>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
    <div id="container" style="width:100%; height:400px;"></div>
    <button id="button" class="autocompare" onclick="plotNewChart()">Add series</button>
    <script type="text/javascript">
         $(function () { $('#container').highcharts({ chart: { events: { addSeries: function () { var label = this.renderer.label('A series was added, about to redraw chart', 100, 120).attr({ fill: Highcharts.getOptions().colors[0], padding: 10, r: 5, zIndex: 8 }) .css({ color: '#FFFFFF' }) .add(); setTimeout(function () { label.fadeOut(); }, 1000); } } }, xAxis: { categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] }, series: [{ data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4] }] }); });
    </script>
</body>
</html>

What I want to do is link another file, myJS.js. I don't know how to do that without the typical <script src="myJS.js"></script>.

3
  • the javascript you linked is server-side. Put that in a .js file in your project root and start it with node filename.js Commented Sep 29, 2014 at 20:50
  • Is there a way to make the javascript client side? Commented Sep 29, 2014 at 20:52
  • I'm not sure if express is capable or even has the potential to run client side. But that's my own experience. Commented Sep 29, 2014 at 20:56

1 Answer 1

23

You need to setup your Express server to serve static files. At the moment, it appears to only serve the '/' route.

Just before you setup your routing, add the line below. You can then serve up static assets from a 'public' folder relative to where your script it.

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

So if you put your myJS.js in public/js/myJS.js you can then reference it like so

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

More info in the docs: http://expressjs.com/api.html#express.static

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

6 Comments

I linked created a new express app, following what I read in the link below and your instructions. However, I think I have to use app.engine('html', require('ejs').renderFile); so as not to have to write the html in "jade". Furthermore, for some reason, even though the css worked when I created the express app out of the box (without adding my static html), the javascript wouldn't link. Do you know why? stackoverflow.com/questions/11710995/…
Oh I see - the layout.jade page had linked the css automatically, so I can link my js through there. Still can't get it with the static, however :|
You need to add: var path = require('path'); otherwise the code will cause an error.
ReferenceError: path is not defined
What if we only want to expose a single file like that statically?
|

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.