Angular2 working with NodeJs - node.js

My understanding is that AngularJS including Angular2 is a client-side framework, while Node.JS is a server side platform. They should not cross. But all Angular2 tutorials I found use Node/NPM. Why is that?

Because npm is a package manager for packages written in JavaScript, and JavaScript can run both on client and server side. In other words, frontend and backend applications can both benefit from packages. Many development tools also use node as an underlying process (e.g. Jest-cli).

I would suggest you to use angular2 only on client side. The performance of angular2 really shines when it comes to handling view containers over regular server side codes. On the NodeJS side, I would recommend using es6 features. Which has revolutionised how you can not only code, but also performance.

The reason you see most tutorials using npm is because they are either using TypeScript or a build tool that uses Node to convert to ES5 or build your project.
However you can still build Angular2 apps using the sfx version (self-executing bundle) of Angular2 and using ES5 syntaxes without having to use node or npm. Here is a blog post that shows how to do that
http://blog.thoughtram.io/angular/2015/05/09/writing-angular-2-code-in-es5.html

Angular2 is a front-end framework but a lot of tooling that assists in building Angular2 applications are available via NPM.
If you want a package manager that's geared specifically to front-end development I suggest JSPM. It supports front-end modules of various formats (ie AMD, CommonJS, UMD), handles transpiling out of the box, and can be used as a build tool to concatenate/minify your application code.
You'll still need NPM for many other utilities and you'll need a back-end server for testing (I recommend live-server).
As far as the back-end is concerned. One of the Angular2 dev teams is working on a Node/Express extension that supports isomorphic rendering of JS. In short, it pre-renders the view on the server so the user can interact with it in the browser while the app loads in the background. The start time of launching a fully-featured SPA will still be kind of slow (relatively) but perceived speed will be instantaneous.

Related

Does the default Reactjs application use node.js?

This may be a dumb question, but is the default react app (created using npm create-react-app my-app) using node.js? I am confused because in my web development class at university, I had to download node.js to create react applications. However, I didn't have to do anything like creating a server or initiating a node.js file, which is described in w3school's node.js tutorial. Because of this, I found out that I don't even really know what node is used for, besides downloading packages like redux and whatnot.
create-react-app uses node.js for all of its dev tooling. Likewise, a lot of the tools you'll use in the React ecosystem (like webpack, prettier, npm), all run on top of node.js as well).
You'll most probably build your react app to a static file, in which case the production output will not require node.js, it will be html and javascript assets that can be served directly to a client.
Hope that helps! Let me know if you have other specific questions
Node.js is used for server-side while React.js is for the front-end part. So, no, you don't need Node.js.
Under development mode, yes. Create react app runs NodeJs with Webpack Dev Server to allow you get feedback when modifing files, start or stop the server.

general question about reactJS, nodeJS and NPM

first, the "facts" that I know:
reactJS is frontend framework, after compiling it'll generate a bundle of html/js/css files that can be recognized by modern browsers, so the browser straightforward request the web host server for these static html files ;
nodeJS is a server-side environment, or sometimes people call it a backend framework, browsers don't understand nodeJS but have to request the web host server to interpret it and to send back the html response.
according to wiki, npm is a package manager for the JavaScript programming language. It is the default package manager for the JavaScript runtime environment Node.js.
my question is:
is my understanding correct
why reactJS also using npm and includes node_module, does it have something to do with nodeJS
Javascript is the programming language used in both Node.js and in the browser, but that's about as far as the similarities goes. Node uses the V8 engine, which is the same running in the chrome browser.
Even though they use the same engine, they are quite different. They both have plenty of APIs - most notably, the browser has the DOM (which allows you to render stuff to the screen). In Node.js you do not have the DOM, but there are other APIs, for accessing the filesystem for instance.
Since both are running JavaScript, you can try to run the same program both in node and in the browser, but it will sometimes fail. React is a front-end framework which uses the DOM, so if you try to run React in node, it will crash because in Node there is no DOM (Node has no screen to render to). If you try to run a program suited for Node.js in the browser, it will crash if it uses some Node-specific API.
The confusing bit is that npm is a package manager for JavaScript, not necessarily node (even though node is in the name..). Therefore, we can use npm also for web applications. (If we do, we need to use a bundler like webpack though).
Some npm modules might work both in the browser and in node, but some will only work on one of them. If the npm module requires some platform-spcific API (DOM for the browser for example) then it will not work for the other platform.
Hope that clears things out! I understand that it's a bit of a mess..

Why Angular need Node Js? and what is role of Angular Cli?

I am a Beginner to Angular technology and came across this question.
why is Node js needed in Angular as Node js is a backend technology?
You need NodeJs for angular if you intend to create a front end server, use typescript or make anything other than a purely in browser application(unless you use another server framework: Apache, nginx, lighttp, ruby/rails etc.). For example Vanilla javascript does not support require or import functionality so you need node to load file dependencies, and angularjs does not allow for server creation on its own. You can also choose to build angular applications in TypeScript which utilises NodeJs.
It is worth noting that technically speaking nodejs and angularjs are separate frameworks, angular can be run without node but would only support limited functionality(no db access, no server etc).
The CLI is needed in order to run angular commands through the command line, to generate services / components etc.
You can read more about angular here
Angular does not need Node. However to make the dependencies management easier especially in package.json , npm which is a package manager is required. And in order to get npm, you need to install Node first.
As for angular cli, it provides a terminal that makes it easy to execute operations like creation of an angular app, angular components, building...

Typescript + Node + Express?

After stumbling upon the typscript-node starter released by microsoft, i started to get lost.
Can we really replace node with typescript on the server? There are several serverside things that TS does well:
- Creating a web API service with express
- Managing the CRUD queries with mongoDB
And much more...
I am used to generate an api with node and connect angular to that api. Am i wrong?
Should we switch to TS on the backend and forget about writing node code on the server?
Typescript is a (or rather, a superset of) language - not a runtime. It is the equivalent of Javascript except it needs to be compiled to run on the Node.js runtime.
You can write the backend with Typescript if you want, and then run it through ts-node, or just compile down to ES6 via tsc and then run it with standard Node (v8+ is recommended). This is what I do with many projects. It is still "node code", it just has all the benefits (and gimmicks) or Typescript.
I recommend the library meseret to manage your typescript node.js backend code. It has support for Koa, Mongoose and Socket.io, with many builtin configurations. It is a great way to manage things in one place, using TypeScript throughout your project.

Angular application requred node.js

I am going to create my first angular application. I have read lots of angular tutorial and many of them mentioned node.js.I am really got confused here.
Do we really need to install node.js and other dependencies to run angular application ?
Can't I just add respective angular libraries in my eclipse and create Angular application?
What are advantages of using node.js?
Also it is necessary to add all angular code is under App folder?
Regards,
Dip
As it is said on note.js website
Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.
So it depends if you want to have server logic (services) or not. If you are creating just the UI of the app you don't need note.js. Also Angular is NOT dependable on note.js

Resources