Angular 2 in production

Asked

Viewed 786 times

0

Eai guys,I have an application in Angular 2 that I want to publish it on the site www.umbler.com hosting server but do not consig, have put on the server in Nodejs but does not work. I don’t know how the build part works, etc. Someone could help me?

  • I didn’t quite understand your question, but you should make your local application to then go up to the server. In the same way as using NPM, Bower, Composer, etc.

  • With angular 2 when Gero build is created the /dist folder where I should put on a Tomcat server for example, as I would do it there?

1 answer

1


To put on a nodejs server:

1- If you are using angular cli run the command to generate the Bundles:

ng build --prod

This will generate the dist folder with all the site static code.

2- Download Node js on your development machine

3-After installing type in the prompt (in the directory a level above the dist folder):

npm init

Answer the console questions (leaving the startup script as index.js)

4- Create an index.js file:

const express = require('express');

const app = express();


app.set('port', 80);


app.use(express.static(__dirname + '/dist'));


app.get('*',function (req, res) {

  res.sendFile(__dirname + '/dist/index.html');

});


app.listen(app.get('port'), function() {

  console.log('Running on port ', app.get('port'));

});

Your directory structure should be:

home
    -dist/
      --arquivos html
    -index.js
    -package.json

To test place run:

npm start 

To run a website on each route:

Whereas there will be two dist-site1 and dist_site2 directories

const express = require('express');

const app = express();


app.set('port', 80);

// Servir estático os dois dists em rotas separadas:
app.use('/site1',express.static(__dirname + '/dist_site1/'));
app.use('/site2',express.static(__dirname + '/dist_site2/'));

// Direcionar para um dos sites caso entre com o link do dominio
app.get('/',function (req, res) {

  res.redirect('/site1');

});

// Direcionar todas as rotas não interceptadas pelo estático para o angular
app.get('/site1/*',function (req, res) {

  res.sendFile(__dirname + '/dist_site1/index.html');

});

// Direcionar todas as rotas não interceptadas pelo estático para o angular
app.get('/site2/*',function (req, res) {

  res.sendFile(__dirname + '/dist_site2/index.html');

});

app.listen(app.get('port'), function() {

  console.log('Running on port ', app.get('port'));

});
  • This worked, thanks, but just for one application, I tried to do the Node manage 2 applications by sending each one through the path, but in this case not the?

  • I added an example in the solution description with comments on each key line.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.