I'm new in Angular and NodeJS. I finished all the basic documentation, and now I'm doing tutorials. My question is about the architecture.
Following the angular tutorial, you create a new server:
ng new new-project
That creates a whole server listening to port 4200, you learn and work with angular, learn about directives, etc.
Then you create a server with node, configure routes, etc.
But how these two servers live together?
What do you recommend me to join them?
This is a node server. The angular part are just two files
This is the server created with ng serve. The angular part is so much complicated
Angular project is not a server. Angular is framework to create front-end page/app. Angular-cli command ng serve is used to build application and start a web server on localhost.
When you build your page with angular use angular-cli command ng build --prod to build your page ( more info about ng build command). The build artifacts will be stored in the dist/ directory of your project.
If you want to host angular page with node - copy file from projektFolder/dist to catalog when node can have access to copy files. In node you can use express library to host static files:
app.use('/myangularproject', express.static('myangularproject')) //host static files`
More info about hosted static files in node and express
EDIT
You use Angular CLI to build an angular application. This is an additional tool for working with angular and you do not have to use it.
Angular cli is a command line interface to scaffold and build angular apps using nodejs style (commonJs) modules. Not only it provides you scalable project structure, instead it handles all common tedious tasks for you out of the box
ng serve
ng serve is a tool from angular cli. When you call this command your project is build in memory and serve it via webpack-dev-server. It is used for quick preview and development of the project. If this command is confusing for you then you can use the npm script npm start.
The CLI supports running a live browser reload experience to users by running ng serve. This will compile the application upon file saves and reload the browser with the newly compiled application. This is done by hosting the application in memory and serving it via webpack-dev-server. doc
ng build
ng build compiles the application into an output directory.
Compiles an Angular app into an output directory named dist/ at the given output path. Must be executed from within a workspace directory.
When you use command ng new Angular CLI add all necessary files to develop your application. Some of them are used to configure the project e.g tslint.json, tsconfig.json, angular.json ... Do not host these files only result files from the use of the ng build command (/dist directory).
Angular CLI compiles your project into several files (try ng build and look how many files do you have in /dist. You must host all of them. These are static files. You do not need a special server like php files. You can host them using a regular file server. I don't know what you concern use in the node to host static files. If they use express you can use express.static(). More info at the top.
Related
We have just a single webpage with some links on clicking them it will redirect to different sources. As of now we are using "npm run build" to create the production package.
But because of the build files having dependencies with node, i cannot host it in a particular server.
Is there a way to create the Reactjs production build without using node ?
I suggest using Netlify to host your react app easily .
Below are some resources that can help you along the way.
https://www.netlify.com/with/react/
https://www.youtube.com/watch?v=sGBdp9r2GSg
You can have a build and upload it manually to your Netlify account,
You can use the CLI (netlify-cli) or you can your account to git .
Similar approach can be followed with git pages for example.
What packages do you have in your package.json file? Did you use a React project template that uses Node server-side features? It seems like you want to host your React project statically, not necessarily get rid of Node and npm.
For example, I've worked on lots of React projects using npm and create-react-app that we were able to host with a .NET backend and Microsoft IIS (instead of Node). The output is .html, .js, and other static files that you can host anywhere.
When you build a react app, the files at folder build contains everything it needs to run
If your hosting server hasn't integration with CI/CD, then you must deploy manually only the build folder, not the root folder (the folder that contains package.json).
I believe your issue is just a confusion/misunderstanding on how react works, how to deploy it, and how to run it.
React needs to be built on an environment where node, npm, and other tools are available. It can be on a build server or in your local machine.
After built, react app is just a folder with a bunch of html, css, js files which will run on the client browser, so, there's no dependency on NODE anymore.
These static files must be served with a simple static file server (apache, nginx, iis, etc),
I recommend you build the app locally on your machine and then deploy manually to your host through ftp, ssh or web interface.
If react is overkill to your needs, then don't use it.
The best approach is to host it in a cloud service that can do the full CI/CD integrated with git, all automated (Google GCP, AWS, Azure, Netlify, etc)
newbie in node and reactjs. I created a sample application with create-react-app. I ran npm run build to build the project and once successfully built saw following .
what does the static server mean here. I am planning to build a app, not just static web pages. will these steps to build and deploy work, if I copy the build folder to my development box and ran the following commands.
do i need to install any web server for it work in a linux dev box? how does the deployment work in nodejs/linux work, compared to like in microsoft box , you have to install IIS web server then deploy the website under it.
The build folder is ready to be deployed.
You may serve it with a static server:
npm install -g serve
serve -s build
In the developing phase you can use npm start to start a dev server that auto reloads on changes. (Pretty neat).
When you want to host your app you should use the npm run build command and deploy it as a static website. You can use a basic Apache Server for that. Your build folder should contain an index.html and various js files that will be loaded when a user requests your website.
Voilá you hosted your react app ;)
Currently I made a simple app with react, it works perfectly and I use react-scripts. I installed React by following the Facebook github page, and it written that "When you're ready to deploy to production, running npm run build will create an optimized build of your app in the build folder. ". After I press npm run build, a folder build created, but I do not know what to do with this folder. I tried some method like, move all folders except build and npm run, but it did not work. Anyone can explain me what is this folder "build" ? Thank you
npm run build builds the app for production to the build folder.
It correctly bundles React in production mode and optimizes the build for the best performance.
The build is minified and the filenames include the hashes.
npm run build creates a build directory with a production build of your app. Set up your favourite HTTP server so that a visitor to your site is served index.html, and requests to static paths like /static/js/main..js are served with the contents of the /static/js/main..js file.
I have created a jhipster client project (yo jhipster --skip-server), my question is how to run or deploy client project.
It's an angular 1 app, so it must be deployed in a web server. In development, gulp serve will run your app. For production run gulp build then copy the contents of target/www folder to your web server. Of course, it still requires a JHipster server application somewhere else.
This option is usually not used by users but rather by JHipster generator itself or by module developers.
I remember hearing from someone that you can simply type in a command to compile a meteor.JS app into a node.JS app, and then deploy it as a node.JS app. Is that correct? Also, does that mean I can build a completely node.JS app by first building it in Meteor, and then using a command to compile it into node? If so, how can I do that?
You can use tool for doing that:
https://github.com/onmodulus/demeteorizer
Tutorial:
http://blog.modulus.io/demeteorizer
I believe you are looking for meteor bundle (See documentation). Meteor bundle constructs a tar.gz file from your meteor application. Once extracted, you should be able to run main.js in the extraction directory and your application built with meteor will run as a standard node application.
For clarification, here are some steps to bundle and run your node application built with meteor.
Within your meteor application directory, run:
meteor bundle appName.tar.gz
Then, unpack the bundle that meteor creates:
tar -xvzf appName.tar.gz
If the proper version of node is installed, you should be able to set your environment variables, and run the application.
export MONGO_URL='mongodb://localhost'
export ROOT_URL='http://myApp.com'
export PORT=5000
And finally, run your application. In the directory that you extracted the tar.gz file to, run:
node main.js