Hướng dẫn how to render html file in javascript - làm thế nào để hiển thị tệp html trong javascript

OK, tôi đang sử dụng STOR JavaScript Sever, bao gồm Node.js. Do các vấn đề về hiệu suất, chúng tôi đã quyết định di chuyển một trang để được hiển thị phía máy chủ, không phải phía máy khách, vì vậy máy chủ trả về một luồng HTML, được hiển thị hoàn toàn, trở lại máy khách.

Tôi đã thấy câu hỏi này và các câu trả lời liên quan, nhưng tự hỏi liệu đây là cách tiếp cận tốt nhất hay đúng. Cụ thể, cách thích hợp nhất để hiển thị một trang và chạy tất cả các javaScript trên đó trong cuộc gọi js hoặc node.js là gì?

Những ý tưởng mà tôi đã xem xét:

  1. Gọi mã JavaScript trực tiếp trên trang và đảo ngược mọi thứ để tạo ra các mục HTML cần thiết. Vì điều này là khẩn cấp, tôi thà tránh viết lại nhiều hơn tôi phải làm.

  2. Kết xuất một tài liệu, với một iframe đơn giản để tạo HTML. Nhưng làm thế nào để tôi trỏ đến trang trong iframe, vì tôi là phía máy chủ? Chắc chắn điều này chỉ là thêm một mức độ trừu tượng khác vào cùng một vấn đề.

  3. Sử dụng các ý tưởng chi tiết ở trên, nhưng tôi tự hỏi liệu đây có phải là con đường phù hợp hay không, với một số vấn đề tôi đã thấy gặp phải với nó.

Chỉnh sửa: Chỉ để làm rõ - Tôi muốn, thực tế, tải trang HTML trong trình duyệt, để nó hoàn thành kết xuất và sau đó chụp toàn bộ HTML được tạo để chuyển qua máy khách (tiết kiệm thời gian để kết xuất trên máy khách).

Là một nhóm các nhà phát triển WordPress, chúng tôi thường vấp phải một nhiệm vụ mà chúng tôi cần yêu cầu một bộ dữ liệu bằng AJAX và kết xuất nó cho trình duyệt.

Hầu hết thời gian chúng tôi làm như thế này:

Nó hoạt động. Tuy nhiên, rất khó để duy trì mã này và hầu hết thời gian, đánh dấu HTML mà chúng ta cần sử dụng phức tạp hơn nhiều so với ví dụ trên. Khi nhiều cập nhật và thay đổi xảy ra trong đánh dấu, chúng tôi thấy ngày càng khó làm việc với mã này.

Nhập WP.Template

Nếu bạn đã quen thuộc với UnderCore.js hoặc {{Mustache}},

$ npm -v
7.6.3 ## installed npm version
3 là một người trợ giúp khuôn mẫu JavaScript dựa trên dấu gạch dưới được vận chuyển với WordPress.

Vì vậy, làm thế nào để chúng ta sử dụng nó?

Tạo và hiển thị mẫu đánh dấu HTML của bạn

Chỉ cần với điều này, chúng ta có thể ngay lập tức thấy việc duy trì đánh dấu HTML của chúng ta dễ dàng như thế nào.

Ghi chú

1. Điều quan trọng là mẫu đánh dấu HTML được bọc với

$ npm -v
7.6.3 ## installed npm version
4. 2.
$ npm -v
7.6.3 ## installed npm version
5 cần được đặt trước với
$ npm -v
7.6.3 ## installed npm version
6. 3.
$ npm -v
7.6.3 ## installed npm version
7 là biến được truyền vào mẫu từ JavaScript. Bạn sẽ thấy điều này trong thời gian ngắn. 4. Logic JavaScript đi vào bên trong
$ npm -v
7.6.3 ## installed npm version
8. 5.
$ npm -v
7.6.3 ## installed npm version
9 là một chức năng nhấn mạnh. Xem dấu gạch dưới.js. 6. Sử dụng
const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

app.get("/", (req, res) => {
  res.send("Hello world!");
});
0 để thoát đầu ra. 7. Sử dụng
const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

app.get("/", (req, res) => {
  res.send("Hello world!");
});
1 cho đầu ra chưa được xử lý. Sử dụng
const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

app.get("/", (req, res) => {
  res.send("Hello world!");
});
0 với dữ liệu có chứa đánh dấu sẽ chuyển đổi các thẻ thành các thực thể HTML. Vì vậy, ví dụ, nếu dữ liệu là
const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

app.get("/", (req, res) => {
  res.send("Hello world!");
});
3 và chúng tôi thực sự muốn hiển thị hình ảnh, chúng tôi cần sử dụng
const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

app.get("/", (req, res) => {
  res.send("Hello world!");
});
1.
2. The
$ npm -v
7.6.3 ## installed npm version
5 needs to be prefixed with
$ npm -v
7.6.3 ## installed npm version
6.
3.
$ npm -v
7.6.3 ## installed npm version
7 is the variable passed to the template from the JavaScript. You’ll see this shortly.
4. JavaScript logic goes inside
$ npm -v
7.6.3 ## installed npm version
8.
5.
$ npm -v
7.6.3 ## installed npm version
9 is an Underscore function. See Underscore.js.
6. Use
const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

app.get("/", (req, res) => {
  res.send("Hello world!");
});
0 for escaping output.
7. Use
const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

app.get("/", (req, res) => {
  res.send("Hello world!");
});
1 for unescaped output. Using
const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

app.get("/", (req, res) => {
  res.send("Hello world!");
});
0 with data containing markup will convert the tags to HTML entities. So for example, if the data is
const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

app.get("/", (req, res) => {
  res.send("Hello world!");
});
3 and we indeed want to render the image, we need to use
const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

app.get("/", (req, res) => {
  res.send("Hello world!");
});
1.

Tải WP-Util

Mã cho

$ npm -v
7.6.3 ## installed npm version
3 nằm trong WP-UTIL, vì vậy chúng tôi cần đảm bảo rằng
const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

app.get("/", (req, res) => {
  res.send("Hello world!");
});
6 được tải.

Bạn có thể làm điều này bằng cách thêm

const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

app.get("/", (req, res) => {
  res.send("Hello world!");
});
7 như một sự phụ thuộc của JavaScript của bạn. Như thế này:
const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

app.get("/", (req, res) => {
  res.send("Hello world!");
});
8
const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

app.get("/", (req, res) => {
  res.send("Hello world!");
});
8

JavaScript

Một vài ghi chú nữa…

1. Chúng tôi bỏ qua

$ npm -v
7.6.3 ## installed npm version
6 trong
const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

app.get("/", (req, res) => {
  res.send(" server Response

This page was render direcly from the server

Hello there welcome to my website

"
);
});
0. 2. Dữ liệu chúng tôi chuyển đến
const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

app.get("/", (req, res) => {
  res.send(" server Response

This page was render direcly from the server

Hello there welcome to my website

"
);
});
1 là biến
$ npm -v
7.6.3 ## installed npm version
7 chúng tôi sử dụng trong mẫu của chúng tôi.
2. The data we pass to
const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

app.get("/", (req, res) => {
  res.send(" server Response

This page was render direcly from the server

Hello there welcome to my website

"
);
});
1 is the
$ npm -v
7.6.3 ## installed npm version
7 variable we use in our template.

Sự kết luận

Mặc dù chúng tôi đã sử dụng

$ npm -v
7.6.3 ## installed npm version
9 trong ví dụ của mình, nhưng bạn nên giảm thiểu logic JavaScript trong mẫu của bạn. Mục tiêu là để phân tách các chế độ xem của người Viking và logic logic.

Ví dụ: bạn có thể tiếp cận ví dụ của chúng tôi như thế này:

JavaScript

Mẫu

$ npm -v
7.6.3 ## installed npm version
3 là một công cụ tốt để tạo mã sạch và có thể bảo trì. Một lợi thế khác của việc sử dụng kỹ thuật này là bạn không cần sửa đổi JavaScript của mình khi bạn sửa đổi mẫu HTML của mình. Là một nhà phát triển, điều đó có nghĩa là không cần thiết lập lại / đóng gói / thu nhỏ / thanh trừng bộ đệm JavaScript.

Bạn nghĩ sao? Những lời khuyên này sẽ giúp bạn một cách tốt hơn, sạch hơn để hiển thị HTML với JavaScript? Để lại cho chúng tôi một bình luận dưới đây. Cần một nhóm để cung cấp các giải pháp như thế này cho dự án trang web của bạn? Liên hệ với chúng tôi hôm nay.

Khi phát triển các ứng dụng web, bạn có thể cần hiển thị các thành phần HTML bên trong máy chủ của bạn. Điều này sẽ giúp tạo các trang tương tác ở phía máy khách sau khi yêu cầu truy cập các trang này được thực hiện.

Có nhiều cách khác nhau để lưu trữ các trang HTML của bạn (trang web), ví dụ:

  • Tự hiển thị phía khách hàng của bạn bằng cách sử dụng các khung như React hoặc,
  • REND các trang trực tiếp từ máy chủ. Khi trình duyệt truy cập một tuyến đường được chỉ định trong máy chủ của bạn, máy chủ sẽ tải các trang HTML này theo yêu cầu của người dùng.

Hướng dẫn này giải thích cách hiển thị các phần tử HTML và các trang HTML trên máy chủ của bạn bằng Node.js.

Điều kiện tiên quyết

Kiến thức cơ bản về việc sử dụng Node.js và Express.js sẽ hữu ích để theo dõi.

Những gì chúng tôi sẽ bao gồm

  • Điều kiện tiên quyết
  • Những gì chúng tôi sẽ bao gồm
  • Đang cài đặt
  • Kết xuất các yếu tố HTML nội tuyến dưới dạng phản hồi HTTP
  • Kết xuất các trang web html dưới dạng phản hồi máy chủ
  • Phân tích dữ liệu biểu mẫu cho máy chủ bằng các biểu mẫu HTML
  • Sự kết luận

Đang cài đặt

  • Kết xuất các yếu tố HTML nội tuyến dưới dạng phản hồi HTTP

$ node -v
v12.18.3 ## installed Node.js version

  • Kết xuất các trang web html dưới dạng phản hồi máy chủ

$ npm -v
7.6.3 ## installed npm version

Phân tích dữ liệu biểu mẫu cho máy chủ bằng các biểu mẫu HTML

  • Sự kết luận
  • Tải xuống Node.js và cài đặt nó. Chạy
    const express = require("express");
    const app = express();
    
    app.listen(3000, () => {
      console.log("Application started and Listening on port 3000");
    });
    
    app.get("/", (req, res) => {
      res.send(" server Response

    This page was render direcly from the server

    Hello there welcome to my website

    "
    );
    });
    5 để kiểm tra nếu cài đặt thành công.
  • Khi Node.js được cài đặt thành công, NPM sẽ được cài đặt cùng. Chạy
    const express = require("express");
    const app = express();
    
    app.listen(3000, () => {
      console.log("Application started and Listening on port 3000");
    });
    
    app.get("/", (req, res) => {
      res.send(" server Response

    This page was render direcly from the server

    Hello there welcome to my website

    "
    );
    });
    6 để xác nhận nếu thực sự một NPM đã cài đặt.

Kiểm tra hướng dẫn này để tìm hiểu thêm về việc sử dụng NPM.

Tạo một thư mục dự án Node.js và khởi tạo dự án trong thư mục này. Sử dụng

const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

app.get("/", (req, res) => {
  res.send(" server Response

This page was render direcly from the server

Hello there welcome to my website

"
);
});
7 để tự động khởi tạo dự án Node.js này.

Cài đặt khung Express.js bằng cách sử dụng

const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

app.get("/", (req, res) => {
  res.send(" server Response

This page was render direcly from the server

Hello there welcome to my website

"
);
});
8. Kiểm tra hướng dẫn này để tìm hiểu thêm về Express.js.: app.js

const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

app.get("/", (req, res) => {
  res.send("Hello world!");
});

Chúng tôi sẽ tạo một máy chủ bằng Express.js. Điều quan trọng là móc máy chủ với Nodemon. Nodemon là một gói tùy chọn (được cài đặt trên toàn cầu) tự động khởi động lại máy chủ sau khi lưu các thay đổi mã phía máy chủ. Đi trước và cài đặt Nodemon bằng

const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

app.get("/", (req, res) => {
  res.send(" server Response

This page was render direcly from the server

Hello there welcome to my website

"
);
});
9. Kiểm tra hướng dẫn này để tìm hiểu thêm về Nodemon.

Kết xuất các yếu tố HTML nội tuyến dưới dạng phản hồi HTTP

Dưới đây là một máy chủ HTTP Hello World đơn giản (

res.sendFile(path [, options] [, fn])
0) nghe trên cổng 3000.

Tên tệp: App.js

const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

app.get("/", (req, res) => {
  res.send(" server Response

This page was render direcly from the server

Hello there welcome to my website

"
);
});

Chạy

res.sendFile(path [, options] [, fn])
1 để khởi động máy chủ.

Hướng dẫn how to render html file in javascript - làm thế nào để hiển thị tệp html trong javascript

Bất cứ khi nào máy chủ đang chạy và truy cập tuyến đường

res.sendFile(path [, options] [, fn])
2, nó sẽ xuất ra văn bản đơn giản
res.sendFile(path [, options] [, fn])
3.

Kết xuất các trang web html dưới dạng phản hồi máy chủ

Phân tích dữ liệu biểu mẫu cho máy chủ bằng các biểu mẫu HTML

Sự kết luận

Tải xuống Node.js và cài đặt nó. Chạy

const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

app.get("/", (req, res) => {
  res.send(" server Response

This page was render direcly from the server

Hello there welcome to my website

"
);
});
5 để kiểm tra nếu cài đặt thành công.

Khi Node.js được cài đặt thành công, NPM sẽ được cài đặt cùng. Chạy

const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

app.get("/", (req, res) => {
  res.send(" server Response

This page was render direcly from the server

Hello there welcome to my website

"
);
});
6 để xác nhận nếu thực sự một NPM đã cài đặt.

Kiểm tra hướng dẫn này để tìm hiểu thêm về việc sử dụng NPM.

res.sendFile(path [, options] [, fn])

Tạo một thư mục dự án Node.js và khởi tạo dự án trong thư mục này. Sử dụng

const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

app.get("/", (req, res) => {
  res.send(" server Response

This page was render direcly from the server

Hello there welcome to my website

"
);
});
7 để tự động khởi tạo dự án Node.js này.

Cài đặt khung Express.js bằng cách sử dụng

const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

app.get("/", (req, res) => {
  res.send(" server Response

This page was render direcly from the server

Hello there welcome to my website

"
);
});
8. Kiểm tra hướng dẫn này để tìm hiểu thêm về Express.js.

Chúng tôi sẽ tạo một máy chủ bằng Express.js. Điều quan trọng là móc máy chủ với Nodemon. Nodemon là một gói tùy chọn (được cài đặt trên toàn cầu) tự động khởi động lại máy chủ sau khi lưu các thay đổi mã phía máy chủ. Đi trước và cài đặt Nodemon bằng

const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

app.get("/", (req, res) => {
  res.send(" server Response

This page was render direcly from the server

Hello there welcome to my website

"
);
});
9. Kiểm tra hướng dẫn này để tìm hiểu thêm về Nodemon.

const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

app.get("/", (req, res) => {
  console.log(__dirname)
});

Kết xuất các yếu tố HTML nội tuyến dưới dạng phản hồi HTTP

Hướng dẫn how to render html file in javascript - làm thế nào để hiển thị tệp html trong javascript

Dưới đây là một máy chủ HTTP Hello World đơn giản (

res.sendFile(path [, options] [, fn])
0) nghe trên cổng 3000.

Tên tệp: App.js

Chạy

res.sendFile(path [, options] [, fn])
1 để khởi động máy chủ.

Bất cứ khi nào máy chủ đang chạy và truy cập tuyến đường

res.sendFile(path [, options] [, fn])
2, nó sẽ xuất ra văn bản đơn giản
res.sendFile(path [, options] [, fn])
3.
: index.html


<html lang="en"> 
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="app.css">
  <link rel="stylesheet"
  href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.no-icons.min.css">
  <title>html formtitle>
head>
<body>
<div class="subscribe-container">
  <form method="POST">
    <input type="email" name="youremail" placeholder="Email Address" required>
    <input type="text" name="yourname" placeholder="Name" required>
    <input class="subscribe-button" type="submit" value="Subscribe">
  form>
div>
body>
html>

Chúng ta có thể sử dụng cùng một máy chủ để hiển thị các phần tử HTML làm phản hồi của máy chủ thay vì gửi văn bản thuần túy.: app.css

.subscribe-container {
    max-width: 800px;
    margin: 60px auto;
    background-color: rgba(130, 184, 219, 0.5);
    border: 5px solid rgb(98, 143, 228);
}
.subscribe-container form {
    display: flex;
    flex-wrap: wrap;
}
.subscribe-container form input {
    margin: 15px;
    border: 1px solid rgb(98, 143, 228);
    padding: 0.4rem;
}
.subscribe-container form input {
    flex: 1 1 200px;
}

.subscribe-container form input[type="submit"] {
    border-radius: 3px;
    background-color: rgba(17, 228, 10, 0.5);
color: rgb(98, 143, 228);
}

.subscribe-container form input[type="email"] {
    flex: 1 1 250px;
}

Hãy để hiển thị tệp vào máy chủ.

const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

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

Hướng dẫn how to render html file in javascript - làm thế nào để hiển thị tệp html trong javascript

Tuy nhiên, điều này không tải kiểu CSS.

const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

app.get("/", (req, res) => {
  console.log(__dirname)
});
9 là một tệp tĩnh. Để tải các tệp máy chủ tĩnh có trong
res.sendFile(path [, options] [, fn])
6 sử dụng

<html lang="en"> 
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="app.css">
  <link rel="stylesheet"
  href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.no-icons.min.css">
  <title>html formtitle>
head>
<body>
<div class="subscribe-container">
  <form method="POST">
    <input type="email" name="youremail" placeholder="Email Address" required>
    <input type="text" name="yourname" placeholder="Name" required>
    <input class="subscribe-button" type="submit" value="Subscribe">
  form>
div>
body>
html>
2 như trong ví dụ dưới đây:

const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

// serve your css as static
app.use(express.static(__dirname));

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

Lưu tệp và mở

res.sendFile(path [, options] [, fn])
2 trong trình duyệt và máy chủ sẽ gửi một trang web như mong đợi.

Hướng dẫn how to render html file in javascript - làm thế nào để hiển thị tệp html trong javascript

Phân tích dữ liệu biểu mẫu cho máy chủ bằng các biểu mẫu HTML

Máy chủ đang chạy. Bây giờ nó đang trả về biểu mẫu HTML như một phản hồi cho máy khách (trình duyệt). Bất cứ khi nào tuyến máy chủ này được truy cập, yêu cầu


<html lang="en"> 
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="app.css">
  <link rel="stylesheet"
  href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.no-icons.min.css">
  <title>html formtitle>
head>
<body>
<div class="subscribe-container">
  <form method="POST">
    <input type="email" name="youremail" placeholder="Email Address" required>
    <input type="text" name="yourname" placeholder="Name" required>
    <input class="subscribe-button" type="submit" value="Subscribe">
  form>
div>
body>
html>
4 sẽ được thực thi từ trình duyệt. Tuy nhiên, điều gì sẽ xảy ra nếu bạn điền vào biểu mẫu này bằng dữ liệu và nhấn nút đăng ký?

Hãy để thử điều đó. Điều này cho thấy kết quả bên dưới, một lỗi được trả về bởi máy chủ.

Hướng dẫn how to render html file in javascript - làm thế nào để hiển thị tệp html trong javascript

Tải lại trang một lần nữa. Mở công cụ Thanh tra Trình duyệt và hướng đến tab Mạng. Điền vào dữ liệu biểu mẫu và nhấp vào nút Đăng ký. Điều này sẽ trả về mã trạng thái 404. Điều này có nghĩa là máy khách không thể gửi/________ 65 dữ liệu đến máy chủ.

Hướng dẫn how to render html file in javascript - làm thế nào để hiển thị tệp html trong javascript

Mẫu HTML chúng tôi đã tạo có phương thức


<html lang="en"> 
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="app.css">
  <link rel="stylesheet"
  href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.no-icons.min.css">
  <title>html formtitle>
head>
<body>
<div class="subscribe-container">
  <form method="POST">
    <input type="email" name="youremail" placeholder="Email Address" required>
    <input type="text" name="yourname" placeholder="Name" required>
    <input class="subscribe-button" type="submit" value="Subscribe">
  form>
div>
body>
html>
5. Điều này có nghĩa là chúng tôi đang gửi yêu cầu

<html lang="en"> 
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="app.css">
  <link rel="stylesheet"
  href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.no-icons.min.css">
  <title>html formtitle>
head>
<body>
<div class="subscribe-container">
  <form method="POST">
    <input type="email" name="youremail" placeholder="Email Address" required>
    <input type="text" name="yourname" placeholder="Name" required>
    <input class="subscribe-button" type="submit" value="Subscribe">
  form>
div>
body>
html>
5 đến máy chủ.

Máy chủ của chúng tôi không có cách xử lý bất kỳ yêu cầu


<html lang="en"> 
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="app.css">
  <link rel="stylesheet"
  href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.no-icons.min.css">
  <title>html formtitle>
head>
<body>
<div class="subscribe-container">
  <form method="POST">
    <input type="email" name="youremail" placeholder="Email Address" required>
    <input type="text" name="yourname" placeholder="Name" required>
    <input class="subscribe-button" type="submit" value="Subscribe">
  form>
div>
body>
html>
5 nào từ máy khách. Máy chủ không cho phép khách hàng

<html lang="en"> 
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="app.css">
  <link rel="stylesheet"
  href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.no-icons.min.css">
  <title>html formtitle>
head>
<body>
<div class="subscribe-container">
  <form method="POST">
    <input type="email" name="youremail" placeholder="Email Address" required>
    <input type="text" name="yourname" placeholder="Name" required>
    <input class="subscribe-button" type="submit" value="Subscribe">
  form>
div>
body>
html>
5 từ tuyến này.

Chúng ta có thể sửa lỗi này bằng cách thêm phương thức


<html lang="en"> 
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="app.css">
  <link rel="stylesheet"
  href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.no-icons.min.css">
  <title>html formtitle>
head>
<body>
<div class="subscribe-container">
  <form method="POST">
    <input type="email" name="youremail" placeholder="Email Address" required>
    <input type="text" name="yourname" placeholder="Name" required>
    <input class="subscribe-button" type="submit" value="Subscribe">
  form>
div>
body>
html>
5 vào tuyến đường. Điều này sẽ xử lý mọi yêu cầu

<html lang="en"> 
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="app.css">
  <link rel="stylesheet"
  href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.no-icons.min.css">
  <title>html formtitle>
head>
<body>
<div class="subscribe-container">
  <form method="POST">
    <input type="email" name="youremail" placeholder="Email Address" required>
    <input type="text" name="yourname" placeholder="Name" required>
    <input class="subscribe-button" type="submit" value="Subscribe">
  form>
div>
body>
html>
5 đến từ tuyến đường này.

$ npm -v
7.6.3 ## installed npm version
0

Khi bạn nhấp vào nút Đăng ký, thông báo

.subscribe-container {
    max-width: 800px;
    margin: 60px auto;
    background-color: rgba(130, 184, 219, 0.5);
    border: 5px solid rgb(98, 143, 228);
}
.subscribe-container form {
    display: flex;
    flex-wrap: wrap;
}
.subscribe-container form input {
    margin: 15px;
    border: 1px solid rgb(98, 143, 228);
    padding: 0.4rem;
}
.subscribe-container form input {
    flex: 1 1 200px;
}

.subscribe-container form input[type="submit"] {
    border-radius: 3px;
    background-color: rgba(17, 228, 10, 0.5);
color: rgb(98, 143, 228);
}

.subscribe-container form input[type="email"] {
    flex: 1 1 250px;
}
2 sẽ được in trên trình duyệt. Hơn nữa, trình duyệt sẽ trả về 200 mã khi kiểm tra mạng Thanh tra, điều này không sao. Máy khách có quyền

<html lang="en"> 
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="app.css">
  <link rel="stylesheet"
  href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.no-icons.min.css">
  <title>html formtitle>
head>
<body>
<div class="subscribe-container">
  <form method="POST">
    <input type="email" name="youremail" placeholder="Email Address" required>
    <input type="text" name="yourname" placeholder="Name" required>
    <input class="subscribe-button" type="submit" value="Subscribe">
  form>
div>
body>
html>
5 và có thể gửi yêu cầu

<html lang="en"> 
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="app.css">
  <link rel="stylesheet"
  href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.no-icons.min.css">
  <title>html formtitle>
head>
<body>
<div class="subscribe-container">
  <form method="POST">
    <input type="email" name="youremail" placeholder="Email Address" required>
    <input type="text" name="yourname" placeholder="Name" required>
    <input class="subscribe-button" type="submit" value="Subscribe">
  form>
div>
body>
html>
5 đến máy chủ.

Hướng dẫn how to render html file in javascript - làm thế nào để hiển thị tệp html trong javascript

Mọi thứ đang hoạt động rất tốt. Tuy nhiên, chúng tôi cần máy chủ lấy dữ liệu biểu mẫu và gửi kết quả có liên quan đến trình duyệt thay vì gửi một số văn bản đơn giản tương đối như

.subscribe-container {
    max-width: 800px;
    margin: 60px auto;
    background-color: rgba(130, 184, 219, 0.5);
    border: 5px solid rgb(98, 143, 228);
}
.subscribe-container form {
    display: flex;
    flex-wrap: wrap;
}
.subscribe-container form input {
    margin: 15px;
    border: 1px solid rgb(98, 143, 228);
    padding: 0.4rem;
}
.subscribe-container form input {
    flex: 1 1 200px;
}

.subscribe-container form input[type="submit"] {
    border-radius: 3px;
    background-color: rgba(17, 228, 10, 0.5);
color: rgb(98, 143, 228);
}

.subscribe-container form input[type="email"] {
    flex: 1 1 250px;
}
2.

Để tương tác với dữ liệu biểu mẫu, chúng tôi cần một gói phân tích cơ thể. Đi trước và cài đặt gói này bằng

.subscribe-container {
    max-width: 800px;
    margin: 60px auto;
    background-color: rgba(130, 184, 219, 0.5);
    border: 5px solid rgb(98, 143, 228);
}
.subscribe-container form {
    display: flex;
    flex-wrap: wrap;
}
.subscribe-container form input {
    margin: 15px;
    border: 1px solid rgb(98, 143, 228);
    padding: 0.4rem;
}
.subscribe-container form input {
    flex: 1 1 200px;
}

.subscribe-container form input[type="submit"] {
    border-radius: 3px;
    background-color: rgba(17, 228, 10, 0.5);
color: rgb(98, 143, 228);
}

.subscribe-container form input[type="email"] {
    flex: 1 1 250px;
}
6. Body-Parser giúp phân tích các cơ quan yêu cầu đến trong một phần mềm trung gian trước khi người xử lý của bạn, có sẵn trong thuộc tính req.body.

Nhập gói bằng hàm

.subscribe-container {
    max-width: 800px;
    margin: 60px auto;
    background-color: rgba(130, 184, 219, 0.5);
    border: 5px solid rgb(98, 143, 228);
}
.subscribe-container form {
    display: flex;
    flex-wrap: wrap;
}
.subscribe-container form input {
    margin: 15px;
    border: 1px solid rgb(98, 143, 228);
    padding: 0.4rem;
}
.subscribe-container form input {
    flex: 1 1 200px;
}

.subscribe-container form input[type="submit"] {
    border-radius: 3px;
    background-color: rgba(17, 228, 10, 0.5);
color: rgb(98, 143, 228);
}

.subscribe-container form input[type="email"] {
    flex: 1 1 250px;
}
7 và nhờ máy chủ sử dụng thông qua
.subscribe-container {
    max-width: 800px;
    margin: 60px auto;
    background-color: rgba(130, 184, 219, 0.5);
    border: 5px solid rgb(98, 143, 228);
}
.subscribe-container form {
    display: flex;
    flex-wrap: wrap;
}
.subscribe-container form input {
    margin: 15px;
    border: 1px solid rgb(98, 143, 228);
    padding: 0.4rem;
}
.subscribe-container form input {
    flex: 1 1 200px;
}

.subscribe-container form input[type="submit"] {
    border-radius: 3px;
    background-color: rgba(17, 228, 10, 0.5);
color: rgb(98, 143, 228);
}

.subscribe-container form input[type="email"] {
    flex: 1 1 250px;
}
8.

Body-Parser có một vài chế độ như:

  • .subscribe-container {
        max-width: 800px;
        margin: 60px auto;
        background-color: rgba(130, 184, 219, 0.5);
        border: 5px solid rgb(98, 143, 228);
    }
    .subscribe-container form {
        display: flex;
        flex-wrap: wrap;
    }
    .subscribe-container form input {
        margin: 15px;
        border: 1px solid rgb(98, 143, 228);
        padding: 0.4rem;
    }
    .subscribe-container form input {
        flex: 1 1 200px;
    }
    
    .subscribe-container form input[type="submit"] {
        border-radius: 3px;
        background-color: rgba(17, 228, 10, 0.5);
    color: rgb(98, 143, 228);
    }
    
    .subscribe-container form input[type="email"] {
        flex: 1 1 250px;
    }
    
    9 - Chuyển tất cả các yêu cầu vào văn bản.
  • const express = require("express");
    const app = express();
    
    app.listen(3000, () => {
      console.log("Application started and Listening on port 3000");
    });
    
    app.get("/", (req, res) => {
      res.sendFile(__dirname + "/index.html");
    });
    
    0 - ​​phân tích dữ liệu vào định dạng JSON.
  • const express = require("express");
    const app = express();
    
    app.listen(3000, () => {
      console.log("Application started and Listening on port 3000");
    });
    
    app.get("/", (req, res) => {
      res.sendFile(__dirname + "/index.html");
    });
    
    1 - Thường được sử dụng khi nhận dữ liệu được đăng từ các biểu mẫu HTML.

Trong ví dụ này, chúng tôi sẽ sử dụng định dạng

const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

app.get("/", (req, res) => {
  res.sendFile(__dirname + "/index.html");
});
1 để tương tác với dữ liệu biểu mẫu.

Body-Parser đi vào bất kỳ tuyến đường nào của bạn bằng cách sử dụng

const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

app.get("/", (req, res) => {
  res.sendFile(__dirname + "/index.html");
});
3 và nhận được phiên bản phân tích cú pháp của các yêu cầu HTTP được thực hiện cho máy chủ.

Với trình phân tích cú pháp cơ thể, chúng ta có thể truy cập dữ liệu biểu mẫu này và tương tác với nó. Hãy cố gắng để bảng điều khiển nhật ký

const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

app.get("/", (req, res) => {
  res.sendFile(__dirname + "/index.html");
});
3 để có được yêu cầu HTTP được phân tích cú pháp:

$ npm -v
7.6.3 ## installed npm version
1

Mở

res.sendFile(path [, options] [, fn])
2. Điền vào đầu vào biểu mẫu và nhấp vào nút Đăng ký. Kiểm tra giao diện điều khiển của bạn.

Hướng dẫn how to render html file in javascript - làm thế nào để hiển thị tệp html trong javascript

Dữ liệu biểu mẫu này có sẵn và bây giờ chúng tôi có thể hướng dẫn máy chủ phải làm gì với nó.

$ npm -v
7.6.3 ## installed npm version
2

Điền vào đầu vào biểu mẫu và nhấp vào nút Đăng ký.

Hướng dẫn how to render html file in javascript - làm thế nào để hiển thị tệp html trong javascript

Lưu ý: Thông tin được lưu trữ trong các biến

const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

app.get("/", (req, res) => {
  res.sendFile(__dirname + "/index.html");
});
6 và
const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

app.get("/", (req, res) => {
  res.sendFile(__dirname + "/index.html");
});
7 tương ứng với
const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

app.get("/", (req, res) => {
  res.sendFile(__dirname + "/index.html");
});
8 và
const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

app.get("/", (req, res) => {
  res.sendFile(__dirname + "/index.html");
});
9.
const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

app.get("/", (req, res) => {
  res.sendFile(__dirname + "/index.html");
});
8 và
const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

app.get("/", (req, res) => {
  res.sendFile(__dirname + "/index.html");
});
8 đặt tên đến từ
const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Application started and Listening on port 3000");
});

// serve your css as static
app.use(express.static(__dirname));

app.get("/", (req, res) => {
  res.sendFile(__dirname + "/index.html");
});
2 của các đầu vào mẫu HTML của bạn. Bằng cách này, bạn có thể sử dụng dữ liệu biểu mẫu và quyết định điều gì xảy ra với từng đầu vào như thể chúng chỉ là thuộc tính của thân đối tượng.

Sự kết luận

Tôi hy vọng hướng dẫn này đã giúp bạn hiểu cách hiển thị dữ liệu HTML vào máy chủ của bạn bằng Express.js.

Các công cụ mẫu như PUG và EJ cũng có thể được sử dụng để hiển thị dữ liệu HTML động vào máy chủ. Cả hai đều biên dịch HTML bằng cách sử dụng các công nghệ được hỗ trợ như Express.js.

Mã hóa hạnh phúc.


Đóng góp đánh giá ngang hàng của: Lalithnarayan C

Làm thế nào để tôi kết xuất trong HTML?

Hàm kết xuất hàm Reactdom.Render () có hai đối số, mã HTML và phần tử HTML. Mục đích của hàm là hiển thị mã HTML được chỉ định bên trong phần tử HTML được chỉ định.The ReactDOM.render() function takes two arguments, HTML code and an HTML element. The purpose of the function is to display the specified HTML code inside the specified HTML element.

Làm cách nào để tham chiếu một tệp HTML bằng JavaScript?

Để bao gồm một tệp JavaScript bên ngoài, chúng ta có thể sử dụng thẻ tập lệnh với thuộc tính SRC.Bạn đã sử dụng thuộc tính SRC khi sử dụng hình ảnh.Giá trị cho thuộc tính SRC phải là đường dẫn đến tệp JavaScript của bạn.Thẻ tập lệnh này phải được bao gồm giữa các thẻ trong tài liệu HTML của bạn.use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the tags in your HTML document.

Bạn có thể viết HTML trong tệp JavaScript không?

Có nhiều cách để viết HTML với JavaScript.tài liệu.Viết chỉ hữu ích khi bạn muốn viết vào trang trước khi nó thực sự được tải.. document. write is only useful when you want to write to page before it has actually loaded.

Làm thế nào để kết xuất hoạt động trong JavaScript?

JavaScript sử dụng mô hình đối tượng tài liệu (DOM) để thao tác các phần tử DOM.Kết xuất đề cập đến việc hiển thị đầu ra trong trình duyệt.DOM thiết lập các mối quan hệ cha mẹ và con cái và các mối quan hệ anh chị em liền kề, trong số các yếu tố khác nhau trong tệp HTML.. Rendering refers to showing the output in the browser. The DOM establishes parent-child relationships, and adjacent sibling relationships, among the various elements in the HTML file.