如何使用 HTTPS 在节点服务器中部署 Sveltekit 应用程序?

San*_*kar 4 node.js svelte sveltekit

我想在 HTTPS 服务器上部署我的 Sveltekit 应用程序。我也有关键文件。这是我的 svelte.config.js 文件

import preprocess from 'svelte-preprocess';
import node from '@sveltejs/adapter-node';
import fs from 'fs';
import https from 'https';
/** @type {import('@sveltejs/kit').Config} **/
const config = {
    // Consult https://github.com/sveltejs/svelte-preprocess
    // for more information about preprocessors
    preprocess: preprocess(),

    kit: {
        // hydrate the <div id="svelte"> element in src/app.html
        target: '#svelte',
        adapter: node(),
        files: { assets: "static" },
        vite: {
          server: {
            https: {
              key: fs.readFileSync("path\\privkey.pem"),
              cert: fs.readFileSync("path\\cert.pem"),
            },
          },
        }
    }
};

export default config;
Run Code Online (Sandbox Code Playgroud)

我应该在哪里保存用于从配置文件中读取的密钥文件?我尝试了一些,发现了一些错误,截图附后。 在此输入图像描述

在此输入图像描述

有人请指导我。提前致谢。

小智 6

我通过使用自定义服务器解决了这个问题

\n
\n

适配器在构建目录 \xe2\x80\x94 index.js 和 handler.js 中创建两个文件。运行index.js \xe2\x80\x94 例如节点构建,如果使用默认构建目录\xe2\x80\x94 将在配置的端口上启动服务器。

\n

或者,您可以导入 handler.js 文件,该文件导出适合与 Express、Connect 或 Polka(甚至只是内置的 http.createServer)一起使用的处理程序,并设置您自己的服务器

\n
\n

可以svelte.config.js保持不变。运行npm run build生成 build 文件夹,server.js在项目根目录中创建一个如下例所示,然后运行node server.js​​.

\n

\r\n
\r\n
import {handler} from \'./build/handler.js\';\nimport express from \'express\';\nimport fs from \'fs\';\nimport http from \'http\';\nimport https from \'https\';\n\nconst privateKey = fs.readFileSync(\'./config/ssl/xx.site.key\', \'utf8\');\nconst certificate = fs.readFileSync(\'./config/ssl/xx.site.crt\', \'utf8\');\nconst credentials = {key: privateKey, cert: certificate};\n\nconst app = express();\n\nconst httpServer = http.createServer(app);\nconst httpsServer = https.createServer(credentials, app);\n\nconst PORT = 80;\nconst SSLPORT = 443;\n\nhttpServer.listen(PORT, function () {\n    console.log(\'HTTP Server is running on: http://localhost:%s\', PORT);\n});\n\nhttpsServer.listen(SSLPORT, function () {\n    console.log(\'HTTPS Server is running on: https://localhost:%s\', SSLPORT);\n});\n\n// add a route that lives separately from the SvelteKit app\napp.get(\'/healthcheck\', (req, res) => {\n    res.end(\'ok\');\n});\n\n// let SvelteKit handle everything else, including serving prerendered pages and static assets\napp.use(handler);
Run Code Online (Sandbox Code Playgroud)\r\n
\r\n
\r\n

\n