nodejs發(fā)布靜態(tài)https服務(wù)器的方法
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);4、啟動服務(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測試一下。
如果能看到,說明服務(wù)正常。
到此這篇關(guān)于nodejs發(fā)布靜態(tài)https服務(wù)器的方法的文章就介紹到這了,更多相關(guān)nodejs靜態(tài)https服務(wù)器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Node.js學(xué)習(xí)教程之HTTP/2服務(wù)器推送【譯】
這篇文章主要給大家介紹了關(guān)于Node.js學(xué)習(xí)教程之HTTP/2服務(wù)器推送的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10
nodejs發(fā)布靜態(tài)https服務(wù)器步驟指南
這篇文章主要為大家介紹了nodejs發(fā)布靜態(tài)https服務(wù)器的步驟指南,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-02-02
Node中node_modules文件夾及package.json文件的作用說明
這篇文章主要介紹了Node中node_modules文件夾及package.json文件的作用說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09
nodejs基礎(chǔ)之buffer緩沖區(qū)用法分析
這篇文章主要介紹了nodejs基礎(chǔ)之buffer緩沖區(qū)用法,結(jié)合實例形式分析了buffer緩沖區(qū)的概念、功能、創(chuàng)建、讀寫等相關(guān)操作技巧,需要的朋友可以參考下2018-12-12

