html5 - Nodejs + Rickshaw: src= reference not working -
i trying make app generates 'live' graphs of twitter word counts, trying extend , exercise in chapter 14 of nodejs in 24 hours. decided use 'rickshaw' , thought i'd start getting simple example working. however, while can simple html page load, can't graph display. firefox debug indicates: "referenceerror: rickshaw not defined [break on error] graph = new rickshaw.graph( { " . means there referencing error after couple of hours of googling , reading, i'm not seeing it. directory structure correct; npm installed modules correctly, no errors. can see i'm missing?
note: new js/node , while book working in express 2.x, have been working in express 3.x, not sure if i've got translation correct in cases. code follows:
package.json
{ "name":"socket.io-twitter-example", "version": "0.0.1", "private": "true", "dependencies": { "express": ">=2.5.4", "rickshaw": ">=1.1.0" } } app.js
var express = require('express'), rickshaw = require('rickshaw'), app = express(), http = require('http'), server = http.createserver(app) server.listen(3000); app.get('/', function (req,res) { res.sendfile(__dirname + '/index.html'); }); index.html
<!doctype html> <html lang="eng"> <head> <meta charset="utf-8" /> <title>socket.io twitter example</title> </head> <body> <h1>rickshaw example</h1> <div id="chart"></div> <ul class="tweets"></ul> <script type="text/javascript" src="node_modules/rickshaw/vendor/d3.v2.js"></script> //don't think "node_modules/" required, doesn't work without either <script type="text/javascript" src="node_modules/rickshaw/rickshaw.min.js"></script> <script> graph = new rickshaw.graph( { element: document.queryselector("#chart"), width: 285, height: 180, series: [{ color: 'steelblue', data: [ { x: 0, y: 40 }, { x: 1, y: 49 }, { x: 2, y: 38 }, { x: 3, y: 30 }, { x: 4, y: 32 } ] }] }); graph.render(); </script> </body> </html>
you need configure express.static middleware express knows static resources js , css files:
app.use(express.static( __dirname + '/public')); its common put such resources public folder (as shown in example above) or static folder.
if create public folder , organize like
app.js public rickshaw vendor d3.v2.js rickshaw.min.js then you'll able have files correctly loaded in html using
<script type="text/javascript" src="/rickshaw/vendor/d3.v2.js"></script> <script type="text/javascript" src="/rickshaw/rickshaw.min.js"></script> see this section of express docs more info on , examples of middleware.
Comments
Post a Comment