简体   繁体   English

在nodejs中从客户端将html发送到浏览器

[英]Sending html to browser from client in nodejs

From: Book - Getting MEAN with Mongo, Express..来自:书籍 - Getting MEAN with Mongo, Express..

Views: HTML responses It's likely that you'll want to respond to many of the requests to your application by sending some HTML to the browser.视图:HTML 响应 您可能希望通过向浏览器发送一些 HTML 来响应对应用程序的许多请求。 By now it will come as no surprise to you that Express makes this easier than it is in native Node.js.到目前为止,Express 使这比在原生 Node.js 中更容易,这对您来说并不奇怪。 Express provides support for a number of different templating engines that make it easier to build HTML pages in an intelligent way, using reusable components as well as data from your application. Express 提供对许多不同模板引擎的支持,这些引擎可以使用可重用的组件以及应用程序中的数据,以智能方式更轻松地构建 HTML 页面。 Express compiles these together and serves them to the browser as HTML . Express 将这些编译在一起,并将它们作为 HTML 提供给浏览器。

They are saying that Express supports many templating engines which can easily send html to browsers.他们说 Express 支持许多模板引擎,可以轻松地将 html 发送到浏览器。
My question here is that if we are using MEAN this means that Angular is used as a frontend and this also means that displaying data on browser should be the responsiblity of Angular.我的问题是,如果我们使用 MEAN,这意味着 Angular 被用作前端,这也意味着在浏览器上显示数据应该是 Angular 的责任。

Nodejs should send data to Angular and Angular should display it on browser. Nodejs 应该将数据发送到 Angular,而 Angular 应该在浏览器上显示它。 If this understanding is correct then why would templating engines of Express be required?如果这种理解是正确的,那么为什么需要 Express 的模板引擎?

Is this quoted paragraph assuming that there is no Angular present therefore the html has to be sent directly to browser from nodejs?这段引用的段落是否假设不存在 Angular,因此必须将 html 直接从 nodejs 发送到浏览器?

Is this quoted paragraph assuming that there is no Angular present therefore the html has to be sent directly to browser from nodejs?这段引用的段落是否假设不存在 Angular,因此必须将 html 直接从 nodejs 发送到浏览器?

You are correct here.你在这里是正确的。 Express can send HTML files in the response, but if you have a separate backend for your application (and many applications do), then Express would send the data to your frontend and Angular would take care of HOW that data is displayed. Express 可以在响应中发送 HTML 文件,但是如果您的应用程序有一个单独的后端(许多应用程序都有),那么 Express 会将数据发送到您的前端,而 Angular 会负责数据的显示方式。

This is an example of Express sending a file on a route request.这是 Express 在路由请求上发送文件的示例。

const express = require('express');
const app = express();
const path = require('path');

app.get('/', (req, res) => {
    res.sendFile(path.join(__dirname + '/index.html'));
});

app.listen(8080);

YES, You are right .是的,你是对的

If Angular is there, All the templates will be built on Angular , So it's enough to pass the data which has be shown using APIs .如果角度是存在的,所有的模板将建立在Angular ,因此,这足以通过已使用要显示的数据APIs If not , You need to render the complete HTML View , So the Template Engine will be helpful.如果没有,您需要呈现完整的HTML View ,因此Template Engine会有所帮助。

In the Paragraph , The are taking about VIEW in MVC , So they are assuming there is no front-end frameworks + API calls .在 Paragraph 中,正在考虑MVC VIEW ,因此他们假设没有front-end frameworks + API calls

There are many ways that you can use an express server.您可以通过多种方式使用快速服务器。

  1. One of a way and also the most common/ traditional way is to use express js as a normal web server which controller get user request then Models deals with the data & finally passing that data to View ( which is MVC ) to render the full html page and send it back to the client.一种方法也是最常见/最传统的方法是使用 express js 作为普通的 web 服务器,控制器获取用户请求,然后模型处理数据,最后将该数据传递给 View(即MVC )以呈现完整的 html页面并将其发送回客户端。

    So it doesn't matter what is your front end, Weather it is Angular, React or Vanilla , express doesn't care about it.所以你的前端是什么并不重要,它是Angular、React 还是 Vanilla ,express 并不关心它。 For express, all 3 types ( Angular, React, Vanilla ) are bunch of JS files & Html file/s.对于 express,所有 3 种类型( Angular、React、Vanilla )都是一堆JS文件和Html文件。 So either you can serve these static files directly as html files or else, you can also use a template engine to serve those files.因此,您可以直接将这些静态文件作为 html 文件提供,或者也可以使用模板引擎来提供这些文件。 Think about a situation where you want do attach some data array to a <script></script> tag of your Angular/ React or Vanilla html file.考虑一种情况,您希望将一些数据数组附加到Angular/React 或 Vanilla html文件的<script></script>标签。 Now you have to use template engine to do that.现在你必须使用模板引擎来做到这一点。

  2. Second way is using express server as an CRUD / RESTfull or Graphql API to serve JSON data.第二种方法是使用 express 服务器作为CRUD/RESTfull 或 Graphql API来提供 JSON 数据。 In this case we keep our static files ( HTML, css, js ) in separate express server which only for serve those files to client.在这种情况下,我们将静态文件(HTML、css、js)保存在单独的快速服务器中,该服务器仅将这些文件提供给客户端。 And separate express Data API for interact with the database.单独的express Data API用于与数据库交互。 So once the static files rendered to browsers, client side JS send request to the express API to get additional data that need to complete the application.因此,一旦静态文件呈现给浏览器,客户端 JS 就会向 express API 发送请求以获取完成应用程序所需的额外数据。

  3. A third way is using express js to server side render (SSR) the Angular / React.第三种方法是使用 express js 来服务器端渲染(SSR) Angular / React。 Long time ago I created an express server to render React JS on server before send to the client.很久以前,我创建了一个快速服务器来在发送到客户端之前在服务器上呈现 React JS。 This way I was able to manage good SEO and give visual to the client as soon as possible while managing single page features ( Similar to how NextJS works ).通过这种方式,我能够管理良好的 SEO 并在管理单页功能的同时尽快为客户提供视觉效果(类似于NextJS 的工作方式)。 With this render server I had separate RESTapi which access to the database.使用这个渲染服务器,我有单独的 RESTapi 可以访问数据库。

Nodejs should send data to Angular and Angular should display it on browser. Nodejs 应该将数据发送到 Angular,而 Angular 应该在浏览器上显示它。 If this understanding is correct then why would templating engines of Express be required?如果这种理解是正确的,那么为什么需要 Express 的模板引擎?

As I said in above, what if you want to attach some data ( Or something else ) from data base to the Angular html file before it send to the client,正如我在上面所说的,如果您想在发送到客户端之前将一些数据(或其他内容)从数据库附加到 Angular html 文件,该怎么办,

<script>
  window.__MY_DATA__ = dataFromDb
</script>

You need to use a template engine.( But not for all use cases. Most of the time not required when using Angular/ React )您需要使用模板引擎。(但并非适用于所有用例。使用 Angular/React 时大部分时间不需要)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM