nodejs發(fā)布靜態(tài)https服務(wù)器的方法
1、先用 npm init 創(chuàng)建一個(gè)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、啟動(dòng)服務(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 目錄下放個(gè)什么 html 文件,比如 test.html 。
用 npm start 啟動(dòng)服務(wù),可以在 Chrome 瀏覽器內(nèi)輸入地址 https://127.0.0.1:8000/test.html
測(cè)試一下。
如果能看到,說(shuō)明服務(wù)正常。
到此這篇關(guān)于nodejs發(fā)布靜態(tài)https服務(wù)器的方法的文章就介紹到這了,更多相關(guān)nodejs靜態(tài)https服務(wù)器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用cluster 將自己的Node服務(wù)器擴(kuò)展為多線程服務(wù)器
nodejs在v0.6.x之后 增加了一個(gè)模塊 cluster 用于實(shí)現(xiàn)多進(jìn)程,利用child_process模塊來(lái)創(chuàng)建和管理進(jìn)程,增加程序在多核CPU機(jī)器上的性能表現(xiàn)。本文將介紹利用cluster模塊創(chuàng)建的多線程的問(wèn)題。2014-11-11利用nodejs監(jiān)控文件變化并使用sftp上傳到服務(wù)器
這篇文章主要介紹了利用nodejs監(jiān)控文件變化并使用sftp上傳到服務(wù)器的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02Node.js版本發(fā)布策略頻率與穩(wěn)定性的平衡
這篇文章主要為大家介紹了Node.js版本發(fā)布策略頻率與穩(wěn)定性的平衡,幫助大家大家更清晰了解node發(fā)展史,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10node錯(cuò)誤處理與日志記錄的實(shí)現(xiàn)
這篇文章主要介紹了node錯(cuò)誤處理與日志記錄的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12