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

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

相关推荐
观无11 分钟前
若依EasyExcel实现单元格合并
开发语言·前端·javascript
xiaohaiAIgeo37 分钟前
【2026年】HG/T 20656-2024化工暖通空调设计规范:新版标准的变化与影响
java·前端·javascript·科普知识
立少→万能汉编40 分钟前
用“立少→超文本”写静态网页,标签<倍>
服务器·前端·javascript
笨鸟先飞,勤能补拙2 小时前
Web 服务器与计算机网络全景原理
运维·服务器·前端·网络·人工智能·计算机网络·web安全
daols882 小时前
vue 甘特图 vxe-gantt 任务条拖拽自动更新日期
javascript·vue.js·甘特图
kisbad2 小时前
Day 036|OpenAI Agents SDK 快速开始:今天跑通第一个 Agent
java·前端·javascript
佳児素花痴╮2 小时前
网络问题与基础理论
运维·服务器·网络
highreport3 小时前
HighReport报表工具定时调度和邮件定时发送
运维·服务器·数据库
TonyLee0173 小时前
Claude Code + DeepSeek:服务器安装与使用笔记
linux·服务器·deepseek·claude code
St_rive3 小时前
Pytest skip&&skipif跳过⽤例
运维·服务器·pytest