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

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

相关推荐
白狐_7981 天前
408计算机网络第3章:以太网交换机与共享式以太网——知识总结
服务器·网络·计算机网络
andr_gale1 天前
02_uniapp自定义上凸效果的底部TabBar
前端·javascript·uni-app·移动端
测试运维日常笔记1 天前
tcpdump(Linux服务器)配合 Wireshark(本地分析)使用手顺与常用过滤语法详解
linux·服务器·tcpdump
倾听醉梦语1 天前
React/Vite/Next.js 前端开发工具 SpotPatch:点击页面元素精准定位 JSX/TSX 源码
javascript·react·ai编程·vite·next.js·前端开发工具
Loongproxy1 天前
大规模采集为啥用隧道代理IP?会话保持如何做到无感轮换
java·服务器·网络
Web极客码1 天前
用 Python 搭建工具调用 Agent 的调试过程
java·服务器·前端
2401_881828321 天前
简易文本处理网页工具|AI 通识课第三次作业
前端·javascript·html
障碍的枫子1 天前
Vue组件导出&渲染
前端·javascript·vue.js
雾时之林1 天前
Linux--介绍及管理
linux·运维·服务器
fhqlongteng1 天前
TCP服务器断开客户端的方法
服务器·网络·tcp/ip