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

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

相关推荐
9分钟带帽5 小时前
linux_通过NFS挂载远程服务器的硬盘
linux·服务器
迷枫7127 小时前
DM8 目录结构与常用排查入口梳理
服务器·数据库
丷丩8 小时前
MapLibre GL JS第19课:实时更新要素
前端·javascript·gis·map·mapbox·maplibre gl js
weixin_394758038 小时前
CRMEB 会员电商系统PRO系统安装之宝塔安装教程-新手推荐(软件管理)
服务器·阿里云
s_w.h8 小时前
【 linux 】动静态库的制作
linux·运维·服务器·算法·bash
专注VB编程开发20年9 小时前
安卓APP与服务器通讯技术,文件传输和文字消息收发
运维·服务器
xiaohua0708day9 小时前
Lodash库
前端·javascript·vue.js
突然好热9 小时前
TS 调试技巧
前端·javascript·typescript
h64648564h9 小时前
Flutter 国际化(i18n)全指南:一键切换中/英/日多语言
前端·javascript·flutter
信息安全失业大专人员10 小时前
HTTP/HTTPS 协议精髓与 WAF(Web 应用防火墙)架构防线大底座
web安全·http·信息安全·https·企业信息安全