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

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

相关推荐
存在的五月雨23 分钟前
Vue3项目一些语法
前端·javascript·react.js
仙柒4151 小时前
管理网络安全
linux·运维·服务器
isyangli_blog1 小时前
静态网站部署方案
服务器
大家的林语冰1 小时前
Node 2026 发布,JS 三大新功能上线,最后一个奇偶版本
前端·javascript·node.js
三*一1 小时前
Mapbox GL JS 自研面要素整形工具开发实录
开发语言·javascript·arcgis·ecmascript
我的世界洛天依1 小时前
胡桃讲编程|续篇!用高数 + JS ES262 硬核解构:求乐正绫的值
javascript
189228048612 小时前
NV243美光MT29F32T08GWLBHD6-24QJES:B
大数据·服务器·人工智能·科技·缓存
洋哥网络科技2 小时前
centos7 升级openssh-10.2
linux·运维·服务器·系统安全
IpdataCloud3 小时前
企业级IP定位服务准确率怎么保证?从数据源到离线库的精度提升指南
运维·服务器·网络·数据库·tcp/ip
treesforest3 小时前
从IP地址归属地查询到IP地理位置精准查询指南
服务器·前端·网络