Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS and Node.js with ExpressJS integration [closed]

I am trying to write an app with Node.js with Express and Angularjs.

Is it possible that I allow AngularJS to handle all landing requests? and Node.js is purely for APIs? meaning that all routes written in Node.js will be like this:

app.get('/api/users', users.showAll);
app.post('/api/users', users.create);
app.get('/api/users/fb/:fbId', users.findByFb);
app.get('/api/users/:userId', users.show);
app.put('/api/users/:userId', users.update);
app.del('/api/users/:userId', users.del);

And AngularJS will be able to resolve URL by itself e.g. localhost/users/Some-Name?

What are some best practices to follow when integrating the two together?

ALso, is it recommended to have AngularJS and Node.js app to be separated into two different application? and what are some ways to do that?

Thanks in advance!!

like image 247
b_o Avatar asked Aug 17 '13 18:08

b_o


People also ask

Can I use NodeJS with AngularJS?

If you are writing an AngularJS front end web application, you may never have to use NodeJS. If you need tooling (build scripts to compile Sass, linters, etc) in your development or deployment process you can use NodeJS task runners like Gulp, Grunt or Webpack.

Is ExpressJS still used 2021?

Express is currently, and for many years, the de-facto library in the Node. js ecosystem. When you are looking for any tutorial to learn Node, Express is presented and taught to people.

Is ExpressJS back end?

js, or simply Express, is a back end web application framework for Node. js, released as free and open-source software under the MIT License. It is designed for building web applications and APIs.

Is AngularJS better than NodeJS?

Node. js is more preferable when faster and scalable web development is needed. It is usually used for building small-sized projects. Angular is preferred when real-time applications, for example, chat apps, or instant messaging are needed.


1 Answers

Obviously, the design and nuances of any application will dictate how exactly it should be setup, but you can certainly have a single page web application running in Angular making AJAX requests back to your node.js server side API. I have created many small and medium sized apps in exactly this way.

Node.js can handle your API in the way you have designed, but you will need some initial route in node that users will initially hit to bring up the HTML page that bootstraps the angular.js application and loads any additional javascript and css libraries that you will need. You will define that route and then setup the static resources in your node application so that the browser can load these things to spin up the angular app.

The angular application itself will be contained in javascript files that will live in a static resources folder in your express app. Typically, something like /js/myNgApp.js will work to start - but you can get more exotic by separating out controllers, directives, services, etc into their own js files. This is something you can do later on.

Once the angular app is loaded and running in the browser, you will just need to use the $resource service in angular to define your ajax urls to the api in node then have the angular client perform get or post (or whatever) request to these urls.

If security is something you will need in the app, that needs to be implemented as a page and url before loading the angular application. There are security frameworks for express that can help you with this. I use passport.

In Chrome, the developer tools (especially the network tab) will become your best friend.

like image 92
BoxerBucks Avatar answered Sep 27 '22 20:09

BoxerBucks