[英]HTTPS node server doesn't return web pages
So I have just turned my HTTP web application to HTTPS.所以我刚刚将我的 HTTP web 申请转到 HTTPS。
So my server.js currently looks like所以我的 server.js 目前看起来像
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这是一个 marko js 项目,我的 server.js 曾经看起来像
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.现在,当我导航到我创建的各种 web 页面中的任何一个时,例如 localhost:8080/home,我只返回你好世界。 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?如何按预期返回我的 web 页面,或者是否有任何资源可以为我指明正确的方向?
did you already take a look at this howto?你已经看过这个howto了吗? https://nodejs.org/en/knowledge/HTTP/servers/how-to-serve-static-files/ 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.它与http
一起使用,但我假设您也可以尝试https
。
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
.浏览器要求什么并不重要,您编写的代码总是以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.如果你想用不同的东西回应,那么你需要注意req
object,看看浏览器要求什么,并用适当的东西回应。
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
.在你的情况下,你总是通过hello world
。 This example shows how to rendering Marko specifically when creating your own http server. 这个例子展示了如何在创建自己的 http 服务器时专门渲染 Marko。
The gist is this.要点是这样的。 Though if you have multiple pages, you'll need to render different templates depending on req.url
.尽管如果您有多个页面,则需要根据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.但是,您似乎正在使用marko-starter
项目。 So for this project, to enable https
you can pass your sslCert
and sslKey
as options to the project config.因此,对于这个项目,要启用https
,您可以将sslCert
和sslKey
作为选项传递给项目配置。
module.exports = require('marko-starter').projectConfig({
sslKey: fs.readFileSync('key.pem'),
sslCert: fs.readFileSync('cert.pem')
/* other config */
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.