0

I am trying load json data using node js for jqgrid. I am using jqgrid and Express Js also. When I run node js it's not loading the json file. Can anyone help me what could be the issue. Here is the node js code snippet:

var express = require("express");
var app     = express();
var path    = require("path");
var config = require('./data.json');

app.get('/',function(req,res) {
    res.sendFile(__dirname + '/griddemo.html')
})

app.use(express.static(__dirname + '/public'))
app.listen(3000);

console.log("Running at Port 3000");
moffeltje
  • 4,521
  • 4
  • 33
  • 57
Anil Samal
  • 1,343
  • 1
  • 13
  • 30

2 Answers2

0

Looks like there is an issue with data.json file path or might be problem with json file. I have tried your script, its working for me. My suggestion is keep an eye on json file path.

ajay
  • 3,245
  • 4
  • 31
  • 59
0

You load your JSON data in the config object, then you do nothing with this object.

I suppose you want to pass this config in the HTML file in order to configure the jqgrid plugin (that I don't know BTW).

In this case you should use a templating language to generate a dynamic page (EJS will be more familiar for you if you have previously done PHP, else you should keep - for now - the default one, jade) and pass your config object to the template you made from your HTML.

To illustrate:

...
app.set('view engine', 'ejs');//Don't forget to install the ejs module
app.get('/',function(req,res) {
  res.render(__dirname + '/griddemo.ejs', {jqgridConfig: config})
})
...

app.js

...
<script>
  var jqgrid_config = <%- JSON.stringify(jqgridConfig) %>;
</script>
...

griddemo.ejs

If you don't already know the express app generator, you should better use it, it will highly help you to understand the concepts you need in order to render a dynamic page with Express.

Another way to do the job without generate dynamic pages is to put data.json in /public and to load this JSON file from griddemo.html (see how to use json file in html code)

Community
  • 1
  • 1
nlc
  • 65
  • 1
  • 5