nodejs发布静态https服务器

1、先用 npm init 创建一个package.json,然后添加依赖 node-static ,package.json 如下:

复制代码
{
  "name": "freeswitch",
  "version": "1.0.0",
  "description": "test freeswitch for webrtc",
  "main": "server.js",
  "dependencies": {
    "node-static": "^0.7.9"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  },
  "keywords": [
    "webrtc"
  ],
  "author": "foruok",
  "license": "ISC"
}

2、执行npm install

3、创建 server.js 文件,内容如下:

复制代码
var fs = require("fs");
var static = require('node-static');

var file = new static.Server('./public');

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

require('https').createServer(options, function (request, response) {
    request.addListener('end', function () {
        file.serve(request, response);
    }).resume();
}).listen(8000);

3、启动服务

复制代码
<!DOCTYPE html>
<html>
  <head>
    <title>test</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="Author" content="foruok" />
    <meta name="description" content="JsSIP based example web application." />
    <style type="text/css">
    </style>
</head>
<body>hello world!</body>
</html>

新建public目录,并且随便在 public 目录下放个什么 html 文件,比如 test.html 。

用 npm start 启动服务,可以在 Chrome 浏览器内输入地址 https://127.0.0.1:8000/test.html测试一下。

如果能看到,说明服务正常。

相关推荐
captain3762 小时前
MyBatis Plus(1)
java·服务器·mybatis
科创致远2 小时前
ESOP 电子作业指导书 WiFi 网络应用实测报告
运维·服务器·网络
重生之小比特2 小时前
【Linux系统】17 库制作与原理(下)
linux·运维·服务器
程序员清风3 小时前
AI 编程助手的权限边界:文件、命令、密钥与人工确认
运维·服务器·网络
wacsii3 小时前
【Linux进程(二)】深入 Linux 内核:进程状态切换与底层数据结构的秘密
linux·服务器
栉甜3 小时前
Vue2学习内容(上)
前端·javascript·vue.js·学习
Rambo.Fan4 小时前
02 · 重建与修复手册(新服务器 playbook)
运维·服务器·django
EVEN-09134 小时前
Linux-IPC进程间通信
linux·运维·服务器
多弗朗皮卡丘4 小时前
Linux 进程信号
linux·运维·服务器
李纲明4 小时前
WordPress 登录后跳回登录页或提示「Cookie 被阻止」:先核站点 URL 与 HTTPS 一致,再查安全插件与 CDN
https·登录·cdn·wordpress·cookie·devdiy·站点url