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测试一下。

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

相关推荐
_Stellar8 分钟前
Linux 服务器管理 根目录文件夹权限设置 基于用户组实现安全共享
linux·服务器·安全
WYiQIU19 分钟前
面了一次字节前端岗,我才知道何为“造火箭”的极致!
前端·javascript·vue.js·react.js·面试
qq_3168377519 分钟前
uniapp 观察列表每个元素的曝光时间
前端·javascript·uni-app
小夏同学呀22 分钟前
在 Vue 2 中实现 “点击下载条码 → 打开新窗口预览 → 自动唤起浏览器打印” 的功能
前端·javascript·vue.js
芳草萋萋鹦鹉洲哦22 分钟前
【vue】导航栏变动后刷新router的几种方法
前端·javascript·vue.js
zero13_小葵司27 分钟前
JavaScript性能优化系列(八)弱网环境体验优化 - 8.3 数据预加载与缓存:提前缓存关键数据
javascript·缓存·性能优化
LUCIFER28 分钟前
驱动开发:详细分析 DTB、DTS、DTSI、DTBO 的区别、用途及它们之间的关系
linux·服务器·驱动开发
Irene199135 分钟前
FileList 对象总结(附:不支持迭代的类数组对象表)
javascript·类数组对象·filelist·不支持迭代
Liu.7742 小时前
vue3 路由缓存导致onMounted无效
前端·javascript·vue.js
BullSmall2 小时前
Test Matrix:测试矩阵(IT 领域定义 + 设计实践 + 华为场景应用)
运维·服务器