主要使用模块
- express
- serve-index
- 指定发布目录
实战代码
vim server.js
mkdir public
"use strict"
var http = require("http");
var https = require("https");
// 主要用来读取证书
var fs = require("fs");
// 核心模块
var express = require("express");
var serverIndex = require("serve-index");
// 创建express服务
var app = express();
// 发布静态路径
app.use(serverIndex("./public"));
app.use(express.static("./public"));
// http server 创建
var http_server = http.createServer(app);
http_server.listen(80, '0.0.0.0');
// 导入证书
var options = {
key: fs.readFileSync("./key.pem"),
cert: fs.readFileSync("./cert.pem")
};
// https server 创建
var https_server = https.createServer(options, app);
https_server.listen(443, '0.0.0.0');
# - 运行代码 -
# 需要安装模块
npm install express
npm install serve-index
# 运行代码
node server.js