简体   繁体   中英

HTTPS node server doesn't return web pages

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?

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

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.

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 */
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM