0

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

3 Answers3

0

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.

Ayzrian
  • 2,279
  • 1
  • 7
  • 14
  • Thank you. Yeah, I'm really trying to get SSL working on my website. It is really hard to find a decent explanation for this. In particular, now I'm looking for a method to render the page along with the headers, while having SSL functioning. – white lotus May 08 '21 at 14:05
  • Sorry am new, can't upvote yet. How do I mark it correct? – white lotus May 09 '21 at 19:04
  • When you hover over my answer, near the upvote and downvote, right below there should be a check icon. – Ayzrian May 09 '21 at 19:17
  • Roger. Checked it. So, can you show me how I am supposed to create https routes? Or, do I just use the App method for that? I have not found the time to try out your solution yet. Just point me at the documentation, please. I need a way to serve the certificate, and a way to render the same headers as with EJS View Engine I was using. I want SSL and use the same (specific) header and footer files with several pages. Does the HTTPS lib cover it? Primarily I just want the best way to host a CA certificate, which certificate I already have. Everything over https and use header/footer like w/ EJS. – white lotus May 11 '21 at 02:07
  • You just need to use the app, and that is it. Create a typical HTTPS server and mount express application to it. Then you can write your express app in the same way as always. Here is similar question https://stackoverflow.com/questions/11744975/enabling-https-on-express-js – Ayzrian May 11 '21 at 02:43
0

Found solution here: [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

Still need to check if my Post routes work...

0

Basically what I have now is:

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 ...

And at the end of the file:

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