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

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

相关推荐
mxd018484 分钟前
sojson。v5:新一代JavaScript代码保护工具的技术解析与应用场景
开发语言·javascript·ecmascript
Michael.Scofield9 分钟前
vue-axios跨域问题
前端·javascript·vue.js
zru_96021 小时前
Vue 常用组件介绍博客
前端·javascript·vue.js
ConardLi1 小时前
MCP + 数据库,一种比 RAG 检索效果更好的新方式!
javascript·数据库·人工智能
m0_616188492 小时前
PDF预览-搜索并高亮文本
开发语言·javascript·ecmascript
JhonKI2 小时前
【Linux】线程池详解及基本实现
linux·运维·服务器
美美打不死2 小时前
webpack js 逆向 --- 个人记录
开发语言·javascript·webpack
我是哈哈hh3 小时前
【Vue】 核心特性实战解析:computed、watch、条件渲染与列表渲染
前端·javascript·vue.js·前端框架·vue·语法基础
前端Hardy3 小时前
HTML&CSS:超好看的收缩展开菜单
javascript·css·html
Riesenzahn3 小时前
你使用过css3的:root吗?说说你对它的理解
前端·javascript