我想呈现一个 node.js 页面,其中 headers 由 http2 提供

I want to render a node.js page with headers served by http2

我找不到答案。

我将如何提供 EJS headers,它们通常位于“views/partials”目录中应用程序根目录,而我正在使用 HTTP2 模块 服务器实例。

这是我的代码:

const http2 = require("http2")
const fs = require("fs")

const server = http2.createSecureServer({
        "key": fs.readFileSync("./ssl/private.pem"),
        "cert": fs.readFileSync("./ssl/cert.pem")
})

server.on("stream", (stream, headers) => {

        stream.respond({
                "content-type": "application/json",
                "status": 200
        })

        stream.end(JSON.stringify({
                "user": "Moi",
                "id": "823"
        }))
})

server.listen(443);
console.log("listening on port 443");

我想实现与此文本块后面的代码相同的事情,但在上面的代码中这样做这个文本块。 在原始代码中,它使用 <%- include('partials/header'); %> 呈现,这发生在“reviews.ejs”文件中。 (这是 rendered 来自 views 目录的文件,该目录还包含 partials 目录,其中包含 header.ejs 文件和 footer.ejs).

所以,我需要复制以下代码:

app.get("/reviews", function(req, res) {
  res.render("reviews"); // I need to replicate this line with HTTP2!
});

我需要复制这个:

app.post("/updated", function(req, res) {
  username = req.body.username;
  email = req.body.email;

  console.log(username, email);
  res.redirect("/");
});

所以,我的最后一个问题是,如何在第一个片段的代码中复制最后两个代码片段? 我的眼睛是方的

(或者 使用 SSL 的任何更好的方法,并像使用 EJS / [=37 的旧站点一样呈现我的新站点=]查看引擎 w/ 部分headers)?


|_|>(我):

谢谢(您可能是个书呆子,无意冒犯)。

您可以通过以下方式将 express 应用挂载到您正在使用的 HTTP 服务器上。

const server = http2.createSecureServer({
    "key": fs.readFileSync("./ssl/private.pem"),
    "cert": fs.readFileSync("./ssl/cert.pem")
}, app); // app is Express application that is a function that can handle HTTP requests.
server.listen(...);

更新:我不确定 Express 是否支持 http2,但我确信如果您需要的是 SSL,相同的代码也适用于 https 模块。

在此处找到解决方案:[https://www.youtube.com/watch?v=USrMdBF0zcg][1]

const express = require('express')
const https = require('https')
const path = require('path')
const fs = require('fs')

const app = express()

app.use('/', (req, res, next) => {
  res.send('Hello from SSL server')
})

// app.listen(3000, () => {})

const sslServer = https.createServer({
  key: fs.readFileSync(path.join(__dirname, 'cert', 'key.pem')),
  cert: fs.readFileSync(path.join(__dirname, 'cert', 'cert.pem'))
}, app)

sslServer.listen(3443, () => console.log('Secure Server on port 3443'))


// SELF-SIGNED CERTIFICATE
// openssl genrsa -out key.pem
// openssl req -new -key key.pem -out csr.pem
// openssl x509 -req -days 365 -in csr.pem -signkey key.pem -out cert.pem

仍然需要检查我的 Post 路线是否有效...

基本上我现在拥有的是:

require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const ejs = require('ejs');
const favicon = require('serve-favicon');
const https = require('https');
const path = require('path');
const fs = require('fs');

const app = express();

const audience_id = process.env.AUDIENCE_ID;
const api_key = process.env.API_KEY;
const api_string = "thisIsNotMyRealAPIKeyPhrase:" + api_key;

const url = "https://us1.api.mailchimp.com/3.0/lists/" + audience_id;

const server = https.createServer({
  key: fs.readFileSync(path.join(__dirname, 'cert', 'key.pem')),
  cert: fs.readFileSync(path.join(__dirname, 'cert', 'cert.pem'))
}, app)

app.use(favicon(__dirname + "/public/images/favicon.ico"));
app.use(express.static("public"));
app.set("view engine", "ejs");

app.use(bodyParser.urlencoded({
  extended: true
}));

app.get("/", function(req, res) {
  res.render("home");
})

// etc ...

并在文件末尾:

server.listen(3443, () => console.log('Secure Server on port 3443'))