Cómo configurar los archivos de índice de renderizado front-end (angula-4 cli) y backend (angular-4 cli) administran ambas aplicaciones angulares.

3
Ketan Chaudhari 27 sep. 2017 a las 07:40

2 respuestas

La mejor respuesta

Establezca este código en su server.js en el lado del nodo, administrar enrutamientos desde el archivo de enrutamiento del lado angular significa definir la ruta como "detalles de administrador / usuario" aquí es mi server.js .

    var express = require('express');
    var app = express();
    var path = require('path');
    var config = require('./server/config/db');
    var bodyParser = require('body-parser');

    app.use(bodyParser.json());

    app.use(bodyParser.urlencoded({ extended: false }));

    app.use(bodyParser.json({limit: '16mb'}));

    /*
        allow cross origin requests
    */
    app.use(function(req, res, next) {
        res.setHeader("Access-Control-Allow-Methods", "POST, PUT, OPTIONS, 
        DELETE, GET");
        res.header("Access-Control-Allow-Origin", 
        "http://localhost:3000");
        res.header("Access-Control-Allow-Headers", "Origin, X-Requested-
        With, Content-Type, Accept");
        res.header("Access-Control-Allow-Credentials", true);
        next();
    });
     /*
        FOR API ROUTES 
     */
    app.use('/', require('./server/routes/api'));
    /*
    FOR INDEX FILE PATH 
    */
    app.use('/root', express.static(path.join(__dirname, '/')));  
    app.use('/backDist',express.static(path.join(__dirname,
    '/admin/dist/')));    
    app.use('/dist',express.static(path.join(__dirname,'/client/dist/')));
    /*
        FOR GET IMAGE PATH 
    */
    app.use('/image',express.static(__dirname+'/admin/src/uploads'));
    /*
      FOR GET ROOT PATH 
    */ 
    app.get('/', function (req, res, err) {
      res.sendFile(path.join(__dirname+'/client/dist','index.html'))
    });
    app.get('/admin/*', function (req, res) {  
      res.sendFile(path.join(__dirname+'/admin/dist','index.html'))
    });
    app.get('/admin', function (req, res) { 
      res.sendFile(path.join(__dirname+'/admin/dist','index.html'))
    });
    /*
       FOR CONNECTION STATUS 
    */
    app.use(function(req, res, next) {  
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
    });

/*
    FOR SET CONNECTION PORT 
*/
app.listen(3000, function () {
console.log('Example listening on port 3000!');
});

module.exports = app; 
0
Ketan Chaudhari 27 sep. 2017 a las 05:03

Establezca Access-Control-Allow-Origin en su express node.js

app.use(function(req, res, next) {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT,DELETE");
    res.set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
    res.set('Access-Control-Allow-Credentials', 'true');
    next();
});
0
Chandru 27 sep. 2017 a las 05:05