5

I'm creating an Express.js API on a Node.js server. The API is used to access data stored on the server. I also keep a log of who's accessing the API in a database.

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

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

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

controller.js

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

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

0

1 Answer 1

10

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

Ofcourse, a browser can't talk to mongodb directly which is why you need an API in the first place, angular would communicate with your API using HTTP.

Angular.js makes an $http call to Node.js which requires and talks to the mongodb.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I was starting to think I would need to use $http to hit a different route in my API to get data but I wasn't 100% and cant find an example of it anywhere. I swear I've been Googleing like a fiend before and after I posted here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.