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

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

相关推荐
阿虎儿25 分钟前
Linux 下为局域网 IP(如 192.168.1.100)打造不受浏览器警告的自签名 SSL/TLS 证书
linux·安全·https
写不来代码的草莓熊33 分钟前
Cesium 地图交互绘制圆形、多边形
前端·javascript·地图
不在逃避q39 分钟前
一步一步学习使用LiveBindings()TListView进阶使用(),打造天气预报程序
服务器·数据库·学习
诸葛老刘1 小时前
Ubuntu 服务器常用运维命令
运维·服务器·ubuntu
红叶舞1 小时前
成数据绑定对象,在应用程序中处理完数据后,将更新的数据序列化为JSON传回远端服务器,很多移动应用使用了这种模式处理服务器端的数据。 ...
运维·服务器·json
深圳恒讯1 小时前
H100服务器是什么?H100服务器适合哪些企业?
运维·服务器
JackSparrow4141 小时前
前端安全之JS混淆+请求加密+请求签名以提升爬虫难度
前端·javascript·后端·爬虫·python·安全
ZJH__GO1 小时前
网络编程v4--群聊和私聊的实现
运维·服务器·网络
Ivan CloudBay2 小时前
SDK 游戏盾如何隐藏游戏服务器的真实 IP?
服务器·tcp/ip·游戏
zcmodeltech2 小时前
智慧农业沙盘模型物联网控制系统设计:基于STM32与Modbus RTU的传感器-执行器闭环方案
服务器·分布式·stm32·嵌入式硬件·物联网·能源