结合 Angular 2 应用服务器和 REST 服务器模块



我正在创建一个新的 Angular 4 应用程序。我遵循了它的官方教程 https://angular.io/tutorial/

使用ng serve命令启动应用程序服务器。

我创建了一个单独的节点js (+express(服务器,该服务器为不同端口上的主应用程序提供REST服务。

有没有办法从同一个nodejs/express服务器为主要的角度应用程序提供服务,这样我就不必为应用程序使用两个服务器?

我使用了打字稿,如果我理解正确的话,在通过nodejs静态提供之前,需要以某种方式将其编译为纯JavaScript?

当然,只需使用 ng build 并提供构建的应用程序的 dist 文件夹,甚至是 src。例:

const staticFor = rel => express.static(path.resolve(__dirname, rel));
const app = express();
if (!config.production) {
app.use('/node_modules', staticFor('../node_modules'));
// In development we do not copy the files over, so map used path
}
app.use('/', staticFor('../dist/app'));
const appPath = path.resolve(
__dirname,
config.production ? '../dist/app/index.html' : '../src/app/index.html'
);
app.get('/yourAppPath/*', (req, res) => {
const markup = fs.readFileSync(appPath, 'utf8');
res.send(markup);
});

不要忘记需要正确的库。

最新更新