简体   繁体   English

如何从现有的 nodejs 服务器提供 Angular 应用程序?

[英]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:脚步:

  1. Do 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 文件夹,您可以轻松地从expressHapiKoa等节点网络服务器框架中提供该文件夹
  2. if you are using express.js you can server angular app.use(express.static(path.join(__dirname, 'dist'))) ;如果您使用 express.js,您可以使用 angular app.use(express.static(path.join(__dirname, 'dist'))) ;
  3. Now use node server URL to serve angular like 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.

相关问题 MEAN:用于Angular App的Node.js服务器-我如何提供角度路线 - MEAN: nodejs server for Angular App - How do i serve angular routes 如何通过部署在 Heroku 上的 Nodejs/Nestjs 服务器为我的 Angular 前端提供服务? - How can I serve my Angular frontend through a Nodejs/Nestjs server deployed on Heroku? 我应该打开服务器的 3000 端口来为 NodeJS 应用程序提供服务吗? - Should I open port 3000 of my server to serve NodeJS application? 如何从作为nodejs服务器的反向代理的nginx服务器提供静态文件? - How do you serve static files from an nginx server acting as a reverse proxy for a nodejs server? 如何将现有的Angular 2应用程序链接到Nodejs Server? - How to link an existing Angular 2 app to Nodejs Server? 如何在不使用 iisnode 的情况下从 IIS/Windows Server Edition OS 提供 NodeJS 应用程序? - How to serve NodeJS application from IIS/Windows Server Edition OS without using iisnode? 如何配置nodejs / expressjs通过https服务页面? - How do I configure nodejs/expressjs to serve pages over https? 如何为我的node-js服务器提供静态文件,这些文件位于应用程序根目录中 - How do I serve static files for my node-js server which are in my application root directory 如何将值从角形式移动到服务器nodejs - How can i move value from angular form to server nodejs 如何将与nodejs和xmpp的聊天集成到我现有的Web应用程序中? - How do I integrate chat with nodejs and xmpp into my existing web application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM