nodejs启动http和https服务(server-index)

let http = require('http');
let https = require('https');
let fs = require('fs');
let express = require('express');
let serveIndex = require('serve-index');

let app = express();
app.use(serveIndex('./public'));
app.use(express.static('./public'));

// http server
let http_server = http.createServer(app);
http_server.listen(811, '127.0.0.1');

// https server
let options = {
    key: fs.readFileSync('./xxxx.dev.key'),
    cert: fs.readFileSync('./xxxx.dev.crt')
}

let https_server = https.createServer(options, app);
https_server.listen(433, '127.0.0.1')

浏览器中访问
http://127.0.0.1:811
https://127.0.0.1:433
都可以访问到node所启动的服务


nodejs启动http和https服务(server-index)_第1张图片
image.png
serve-index是一个NodeJS模块,功能是将文件夹中文件列表显示到浏览器中,可以通过NPM安装。
npm install serve-index --save

你可能感兴趣的:(nodejs启动http和https服务(server-index))