gpt4 book ai didi

angular - 使用公共(public)节点 js 服务器运行多个 angular 4 应用程序(渲染)

转载 作者:行者123 更新时间:2023-12-05 07:40:08 26 4
gpt4 key购买 nike

如何设置渲染索引文件前端(angula-4 cli)和后端(angular-4 cli)管理两个 Angular 应用。

最佳答案

Set this code in your server.js in node side, manage routings from angular side routing file means define path like "admin/userdetails" here is my 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;

关于angular - 使用公共(public)节点 js 服务器运行多个 angular 4 应用程序(渲染),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46439619/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com