[英]How do I serve an Angular application from existing nodejs server?
I have an Angular 6 application and an existing nodejs api application.我有一个 Angular 6 应用程序和一个现有的 nodejs api 应用程序。 So far I have used到目前为止我已经使用了
ng serve
to run and build the angular application.运行和构建角度应用程序。
I now want to serve my angular application from the existing node js server.我现在想从现有的 node js 服务器为我的 angular 应用程序提供服务。
How do I do that ?我怎么做 ? I can't find any documentation.我找不到任何文档。
Steps:脚步:
ng build
, It will create a dist folder which you can easily serve from node webserver framework like express
, Hapi
or Koa
进行ng build
,它将创建一个 dist 文件夹,您可以轻松地从express
、 Hapi
或Koa
等节点网络服务器框架中提供该文件夹app.use(express.static(path.join(__dirname, 'dist')))
;如果您使用 express.js,您可以使用 angular app.use(express.static(path.join(__dirname, 'dist')))
;http://localhost:nodeport
现在使用节点服务器 URL 来提供像http://localhost:nodeport
If you are using Hapi: check this out https://hapi.dev/tutorials/servingfiles/?lang=en_US如果您正在使用 Hapi:请查看https://hapi.dev/tutorials/servingfiles/?lang=en_US
================================basic express server================ ================================基本快递服务器============== =
const express = require('express');
const app = express();
const path = require("path");
const fs = require("fs");
//const bodyParser = require('body-parser');
app.use(express.static(path.join(__dirname, 'dist')));
//app.use(bodyParser.json());
//app.use('/api/v1/', require('./api/routes'));
app.listen(8080,function(err){
if(!err){
console.log("server is running at port:8080);
}
})
You have two ways of serving an Angular SPA:你有两种服务 Angular SPA 的方式:
Usually dev : the Webpack-run server, which is ng serve
.通常 dev :Webpack 运行的服务器,即ng serve
。 Dynamic in the sense that any modification to a file starts a rebuild and updates the output.动态,对文件的任何修改都会启动重建并更新输出。
Usually prod : you build all the html/js files (with ng build [...]
) for the SPA to be statically served by a node
server.通常 prod :您为 SPA 构建所有 html/js 文件(使用ng build [...]
)由node
服务器静态提供服务。
In your case, if you'd like to use an existing node
server, it means you'll have to build the files (with ng build
) and then hook up the usual node
static files serving snippet in your node
app.在您的情况下,如果您想使用现有的node
服务器,这意味着您必须构建文件(使用ng build
),然后在您的node
应用程序中连接通常的node
静态文件服务片段。
Beware though: you'll have to do a full build each time you want to update the display.但请注意:每次要更新显示时,您都必须进行完整构建。 So it's ok if it's not that often, but not ok for a dev environment I guess.所以如果不那么频繁也没关系,但我想对于开发环境来说就不行了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.