Search code examples
node.jssslrenderinghttp2partials

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


I couldn't find the answer.

How would I serve EJS headers, which normally are in the "views/partials" directory in the application root, while I'm using HTTP2 module server instance.

Here is my code:

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");

I want to achieve the same thing as in the code following this block of text, but then doing that in the code above this block of text. Where in the original code it is rendered using <%- include('partials/header'); %> which happens in the "reviews.ejs" file. (Which is the file which is rendered from the views directory which also holds the partials directory, which holds the header.ejs file and the footer.ejs).

So, I need to replicate the following code:

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

And I need to replicate this:

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

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

So, my final question is, how do I replicate those last two snippets of code, in the code of the 1st snippet? cough cough My eyes are square cough

(Or any better way to employ SSL, and render my new site like the old one which used EJS / view engine w/ the partial headers)?


|_|> (me):

Thanks (and you're probably a nerd, no offense).


Solution

  • You can mount the express application to the HTTP server you are using the following way.

    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(...);
    

    Update: I am not sure that Express supports http2 but I am sure that the same code will work for https module if the thing you need is SSL.