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

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

相关推荐
ly76891 小时前
Vue 3 从入门到工程实践:基础语法、组件化、路由、状态管理与项目部署
前端·javascript·vue.js
当代红领巾1 小时前
VMware 里装好 Linux 系统没有 ipv4 地址?
linux·运维·服务器
huashengzsj1 小时前
企业OV泛域名证书价格多少?2026年市场报价与选购指南
网络协议·http·https
qq_401700412 小时前
TCP 粘包问题深度解析:嵌入式网络开发的常见坑与解决方案
服务器·网络·qt·网络协议·tcp/ip
四千岁2 小时前
抓包LangChain-了解LangChain的原理
前端·javascript·后端
用户298698530142 小时前
使用 JavaScript 在 React 中实现 Word 转 PDF
前端·javascript·react.js
DigitalOcean2 小时前
面向 AI 工作负载,单核性能提升 30%:DigitalOcean v5 Droplets 云服务器正式上线
服务器
悟空瞎说2 小时前
UICollectionViewLayout 全套源码 + 逐行中文注释 + 使用场景说明
javascript
谢亮_vipxieliang2 小时前
手机号验证技术方案:号段规则与正则表达式
java·服务器·spring boot·正则表达式·hibernate
guo_wen_qiang3 小时前
mac中iTerm有没有类似xshell远程连接服务器的用户名密码方式,不用每次都要输入用户密码
运维·服务器·macos