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

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

相关推荐
宿6745 分钟前
tsconfig.node.json
前端·javascript·vue.js
QWEDDRFTG5 分钟前
家用服务器供电
服务器
Eloudy20 分钟前
给 Linux 加新硬盘的挂载命令
linux·运维·服务器
lilian23324 分钟前
Harmony os 技术实战|拼豆制图10:把取消、解析失败和保存失败写成可恢复状态机
java·javascript·华为·harmonyos
三言老师32 分钟前
ss高效查看网络连接端口实操
linux·运维·服务器·网络
三言老师34 分钟前
traceroute路由追踪实操
linux·运维·服务器·网络
huabuyu43 分钟前
SourceMap:从「看不懂线上报错」到「让 AI 定位真根因」
前端·javascript
এ慕ོ冬℘゜1 小时前
前端树形二级列表渲染 + 页面传参完整实战解析(附原生jQuery源码)
前端·javascript·jquery
TlSfoward1 小时前
DLL 指纹库 采集、检索 TLSFOWARD tls指纹库
爬虫·网络协议·https·自动化
Bobolink_1 小时前
国际专线的两种隔离方式:物理独占和逻辑隔离的区别
服务器·网络·iplc·iepl