0

I'm trying to create a webpage that will make requests into a neo4j database.

I do the request from a javascript file /projectFolder/js/query.js:

function submitQuery() {
  var r = require("request");
  var txUrl = "http://localhost:7474/db/data/transaction/commit";

  //defines the cypher query
  var query="MATCH (n:Word) RETURN n, labels(n) as l LIMIT {limit}"
  var params={limit: 10}
  var cb=function(err,data) { console.log(JSON.stringify(data)) }

  r.post({uri:txUrl,
          json:{statements:[{statement:query,parameters:params}]}},
          function(err,res) { cb(err,res.body)})

}

and in my projectFolder/index.html I have:

.
.
.
<script data-main="js/query.js" src="js/libs/require-2.1.20.min.js"></script>
<head>
    <script type="text/javascript" src="query.js"></script>
</head>
.
.
.

When I call the submitQuery() function it doesn't go through the var r = require("request"); line.

I've searched for some related issues and ended up trying inserting

require.config({
    baseUrl: './js/',
    paths: {
        request:'libs/require',
        },
});

at the beginning of my query.js, but got the same output.

Can someone give me a light? Thank you!

Lucas Azevedo
  • 1,867
  • 22
  • 39

1 Answers1

1

You are trying to use a node.js module ("request") from a browser. Browsers do not natively support node.js, and do not have a require function.

If you really need to use node.js modules, you should look into using browserify to bundle up your node.js client code into a script that can run in a browser.

Otherwise, this example that uses jQuery may be helpful.

cybersam
  • 63,203
  • 6
  • 53
  • 76