微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我想用 http2 提供的标头呈现 node.js 页面

如何解决我想用 http2 提供的标头呈现 node.js 页面

我找不到答案。

当我使用 HTTP2 模块时,我将如何提供 EJS 标头,这些标头通常位于应用程序根目录的“views/partials”目录中 服务器实例。

这是我的代码

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文件中。 (这是从 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",res) {
  username = req.body.username;
  email = req.body.email;

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

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

(或者任何更好的使用 SSL方法,并将我的新网站呈现为使用 EJS / 查看引擎 w/ 部分标题)?


|_|>(我):

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

解决方法

您可以通过以下方式将 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,但我确信如果您需要的是 https,相同的代码将适用于 SSL 模块。

,

在这里找到解决方案:[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.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

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

,

基本上我现在拥有的是:

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,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'))

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。