0

So I have just turned my HTTP web application to HTTPS.

So my server.js currently looks like

const https = require('https');
const fs = require('fs');

const options = {
  key: fs.readFileSync('key.pem'),
  cert: fs.readFileSync('cert.pem')
};

https.createServer(options, function (req, res) {
  res.writeHead(200);
  res.end("hello world\n");
}).listen(8080);

This is a marko js project and my server.js used to look like

require("./project").server({
  httpPort: process.env.PORT || 8080 // Optional, but added here for demo purposes
}); 

Now currently when I navigate to any of the various web pages I have created such as localhost:8080/home I am only returned hello world. I assume this is due to the response I have in my create server method.

How do I go about returning my web pages as intended or would there be any resources that could point me in the right direction?

Edward Muldrew
  • 253
  • 1
  • 6
  • 20
  • Since you say you turned HTTP into HTTPS: Just use the same code you had for HTTP! Just use `https` instead of `http` in the code. (Or even better: Leave it at HTTP and use a reverse proxy such as [Caddy](https://caddyserver.com/) to handle the SSL termination for you. Much less headache and no certificate fiddling.) – CherryDT Apr 20 '20 at 15:38

3 Answers3

0

did you already take a look at this howto? https://nodejs.org/en/knowledge/HTTP/servers/how-to-serve-static-files/

http.createServer(function (req, res) {
  fs.readFile(__dirname + req.url, function (err,data) {
    if (err) {
      res.writeHead(404);
      res.end(JSON.stringify(err));
      return;
    }
    res.writeHead(200);
    res.end(data);
  });
}).listen(8080);

It's working with http but I'd assume you can try https as well.

Just read the security concerns in the howto before going public with your server ;)

Konstantin A. Magg
  • 1,106
  • 9
  • 19
0

This is the code you have to handle requests:

https.createServer(options, function (req, res) {
  res.writeHead(200);
  res.end("hello world\n");
}).listen(8080);

It doesn't matter what the browser asks for, the code you've written always responds with hello world.

If you want to respond with something different, then you need to pay attention to the req object, see what the browser is asking for and respond with something appropriate.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

I am only returned hello world. I assume this is due to the response I have in my create server method.

If you are creating a server yourself, you're in charge of passing the content to the responses. In your case, you're always passing hello world. This example shows how to rendering Marko specifically when creating your own http server.

The gist is this. Though if you have multiple pages, you'll need to render different templates depending on req.url.

const page = require('./path/to/page.marko');
https.createServer(options, function (req, res) {
  res.setHeader('Content-Type', 'text/html');
  page.render({ data:123 }, res);
}).listen(8080);

However, it looks like you're using the marko-starter project. So for this project, to enable https you can pass your sslCert and sslKey as options to the project config.

module.exports = require('marko-starter').projectConfig({
  sslKey: fs.readFileSync('key.pem'),
  sslCert: fs.readFileSync('cert.pem')
  /* other config */
});

mlrawlings
  • 711
  • 4
  • 9