I'm trying to access data from data.json in my Node.js Express app but am unable to do so.
My project looks as follow:
app.js
var express = require('express');
var app = express();
var d3 = require("d3");
//set which viewengine is used
app.set('view engine', 'ejs');
//serving static files in Puclib folder
app.use(express.static(__dirname + '/public'));
//render index.ejs from ./views when visiting localhost:3000
app.get('/', function(req, res){
res.render('index');
});
//use port 3000
app.listen(3000);
Within index.ejs I am trying to use css and js by linking to them as you normally would.
In my main.js file I am trying to load in data.json I got stored in public/data/
window.onload = function () {
console.log('Js is working');
d3.json("data/data.json", function (error, data){
console.log(data);
})
}
I am able to see the "js is working" log in the console but I am not able to find data.json
My project folder is structured like this:
/public
/css
style.css
/data
data.json
/js
main.js
/views
index.ejs
app.js