1、先用 npm init 創(chuàng)建一個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、執(zhí)行npm install?
3、創(chuàng)建 server.js 文件,內(nèi)容如下:
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、啟動服務(wù)
<!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 啟動服務(wù),可以在 Chrome 瀏覽器內(nèi)輸入地址?https://127.0.0.1:8000/test.html
測試一下。文章來源:http://www.zghlxwxcb.cn/news/detail-695599.html
如果能看到,說明服務(wù)正常。文章來源地址http://www.zghlxwxcb.cn/news/detail-695599.html
到了這里,關(guān)于nodejs發(fā)布靜態(tài)https服務(wù)器的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!