Node.js + Angular = Not Open ReferenceError: require not defined

I am creating an Express.js API on a Node.js server. The API is used to access data stored on the server. I also keep the API access log in the database.

I am trying to create an admin section that Angular.js will use to display admin access logs neatly. I used the Angular Express Bootstrap semester to start my project:

https://github.com/jimakker/angular-express-bootstrap-seed/

My problem is that I need controllers.js to access node modules, but it doesn't seem to know that node exists. Here is my mistake:

controller.js var mongo = require('mongodb'); [Uncaught ReferenceError: require is not defined] 

How can I use node modules in Angular.js files?

+4
source share
1 answer

Node is a server technology, you usually do not use your node modules in a browser with Angular.js. However, if you want commonjs functions to require functionality in the browser, see https://github.com/substack/node-browserify .

Of course, the browser cannot talk directly to mongodb, so you need an API in the first place, angular will communicate with your API using HTTP.

Angular.js calls the $ http call to Node.js, which requires and talks to mongodb.

+10
source

Source: https://habr.com/ru/post/1496664/


All Articles