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

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

相关推荐
૮・ﻌ・21 分钟前
Vue3:组合式API、Vue3.3新特性、Pinia
前端·javascript·vue3
前端不太难22 分钟前
RN + TypeScript 项目越写越乱?如何规范架构?
前端·javascript·typescript
JS_GGbond29 分钟前
用美食来理解JavaScript面向对象编程
开发语言·javascript·美食
Dxy123931021631 分钟前
Python的正则表达式入门:从小白到能手
服务器·python·正则表达式
小周学学学38 分钟前
vSphere DRS与vSphere HA
运维·服务器·vmware·虚拟化
苏打水com40 分钟前
第十六篇:Day46-48 前端安全进阶——从“漏洞防范”到“安全体系”(对标职场“攻防实战”需求)
前端·javascript·css·vue.js·html
德迅云安全—珍珍1 小时前
主机安全-德迅卫士
linux·服务器·安全
咕噜企业分发小米1 小时前
如何平衡服务器内存使用率和系统稳定性?
java·服务器·前端
2301_805962931 小时前
Windows连接腾讯云服务器
服务器·windows·腾讯云
云动课堂1 小时前
一键升级 OpenSSH 10到最新版:告别手工编译、兼容国产系统、批量部署无忧!
linux·服务器·centos