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

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

相关推荐
lightgis18 小时前
16openlayers加载COG(云优化Geotiff)
前端·javascript·html·html5
小飞大王66618 小时前
TypeScript核心类型系统完全指南
前端·javascript·typescript
她是太阳,好耀眼i1 天前
Nvm 实现vue版本切换
javascript·vue.js·ecmascript
蒲公英10011 天前
在wps软件的word中使用js宏命令设置表格背景色
javascript·word·wps
一枚前端小能手1 天前
📜 `<script>`脚本元素 - 从加载策略到安全性与性能的完整指南
前端·javascript
掘金安东尼1 天前
TypeScript为何在AI时代登顶:Anders Hejlsberg 的十二年演化论
前端·javascript·面试
面向星辰1 天前
扣子开始节点和结束节点
java·服务器·前端
执携1 天前
Vue Router (命名视图)
前端·javascript·vue.js
一匹电信狗1 天前
【C++】封装红黑树实现map和set容器(详解)
服务器·c++·算法·leetcode·小程序·stl·visual studio
大锦终1 天前
【Linux】网络层与数据链路层中重点介绍
linux·运维·服务器·网络