Question

I have a server in Node.js and I use Express to make a web app. My server (app.js) gets data from a form (by Ajax post method) and I want that it proceeds this data by using the code of another file (anotherfile.js).

I used this solution: https://stackoverflow.com/a/950146/3303704 and I included this code in my app.js file:

app.post('/', function(req, res) {

$.getScript("anotherfile.js", function(){
alert("Script loaded and executed.");
});

});

But Node.js returns me that "has no method 'getscript'".

Idea of the cause and solution? Thanks!

Était-ce utile?

La solution

You appear to be trying to use jQuery in node.

The solution you have linked to is a solution for front-end.

Use require() in node.

Try reading this article: Node.js, Require and Exports. It explains that for instance if you define this in one file called user.js:

var User = function(name, email) {
  this.name = name;
  this.email = email;
};
module.exports = User;

You can do this in another:

var user = require('./user');

var u = new user.User();
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top