Nodejs将服务器HTTP更改为HTTPS [英] Nodejs Change server HTTP to HTTPS

查看:113
本文介绍了Nodejs将服务器HTTP更改为HTTPS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将我的服务器更改为HTTPS,但无法正常工作。
我的页面显示为我创建了一个包含任何内容的新服务器。
在这种情况下,我有2个文件,app.js带有 express required和routes,而我的server.js执行服务器需要 ./ app

I try change my server to HTTPS, but does not work fine. My page show like I create a new server with anything. In this case I have 2 files, app.js with express required and routes, and my server.js to execute the server require the ./app

我的app.js(服务器端):

My app.js (server-side):

var express = require('express'); // app server
var bodyParser = require('body-parser'); // parser for post requests
var http = require('http');
var https = require('https');
//all endpoints is inside my app.js
var app = express();

// endpoints here
//code .. code with routes, code...

module.exports = app;

我的server.js(服务器端):

My server.js (server-side):

    var fs = require('fs')
    var server = require('./app');
    var port = process.env.PORT || process.env.VCAP_APP_PORT || 3000;


    var options = {
      key: fs.readFileSync('certificates/xxx.key'),
      cert: fs.readFileSync('certificates/xxx.cer')
    };

    server.listen(port, options, function() {
      console.log('Server execute here %d', port);
    });

也许是一个新手问题,我只能用一个文件创建一个服务器,但我不知道如何用这种情况创建......

Maybe is a newbie question, I can create one server with one file only, but I dont know how to create with this case...

有人可以帮我解释一下我在这个案例中做错了吗?

Someone can help me please and explain what I did wrong with this case use?

推荐答案

在您的情况下,您从 app.js 中导出的内容是一个快递 app 对象。这不是从中创建https服务器的正确对象。相反,您需要手动创建https服务器,然后将您的快速应用与之关联。您可以在此处查看Express文档: https://expressjs.com/en /api.html#app.listen

In your case, what you are exporting from app.js is an express app object. That's not the right kind of object to create an https server from. Instead, you need to manually create an https server and then associate your express app with that. You can see the Express doc for doing that here: https://expressjs.com/en/api.html#app.listen.

如果查看 app.listen()的代码 Express存储库中,你会发现它只是这样:

If you look at the code for app.listen() in the Express repository on Github, you will see that all it does is this:

app.listen = function listen() {
  var server = http.createServer(this);
  return server.listen.apply(server, arguments);
};

因此,创建 http 服务器,无法创建 https 服务器。

So, it is hard-wired to create an http server and has no way to create an https server.

要创建https服务器,您必须自己创建服务器并指定 app 对象作为它的请求处理程序。

To create an https server, you must create the server yourself and specify the app object as the request handler for it.

一般方案是这个:

var express = require('express');
var https = require('https');
var app = express();
var options = {...};   // read certificates in here

https.createServer(options, app).listen(443);

请注意,您手动使用 https 模块创建https服务器对象,然后将Express对象与作为请求处理程序的对象关联。 Express中的 app.listen()界面不提供https服务器创建,因此您必须自己完成。

Note that you manually use the https module to create the https server object and then you associate your Express object with that as the request handler. The app.listen() interface in Express does not offer https server creation so you have to do it yourself.

如果你真的想使用你的两个文件,那么你可以这样做:

If you really want to use your two files, then you could do this:

app.js

var express = require('express'); // app server
var bodyParser = require('body-parser'); // parser for post requests
//all endpoints is inside my app.js
var app = express();

// endpoints here
//code .. code with routes, code...

module.exports = app;

server.js

server.js

var fs = require('fs')
var app = require('./app');
var port = process.env.PORT || process.env.VCAP_APP_PORT || 443;
var https = require('https');


var options = {
  key: fs.readFileSync('certificates/xxx.key'),
  cert: fs.readFileSync('certificates/xxx.cer')
};

https.createServer(options, app).listen(port);

这篇关于Nodejs将服务器HTTP更改为HTTPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆