日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【Node.js】serve 实现目录浏览服务

發布時間:2024/9/19 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Node.js】serve 实现目录浏览服务 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言

  • node.js v12.18.2
  • 用 node.js 實現目錄瀏覽服務 (http-server)
  • 前提,已經按照好Node.js
  • serve 官網:https://www.npmjs.com/package/serve
  • github : https://github.com/vercel/serve-handler

下載安裝serve

shell> npm install -g serve D:\sde\nodejs\node-v12.18.2-win-x64\serve -> D:\sde\nodejs\node-v12.18.2-win-x64\node_modules\serve\bin\serve.js + serve@11.3.2 added 78 packages from 39 contributors in 211.874s

啟動serve

命令

serve folder_name
  • folder_name 可以省略。省略時,使用路徑 ./ 。

例如:

shell> serve┌───────────────────────────────────────────────────┐│ ││ Serving! ││ ││ - Local: http://localhost:5000 ││ - On Your Network: http://192.168.0.165:5000 ││ ││ Copied local address to clipboard! ││ │└───────────────────────────────────────────────────┘

效果如下:

可以作為中間件使用

const handler = require('serve-handler'); const http = require('http');const server = http.createServer((request, response) => {// You pass two more arguments for config and middleware// More details here: https://github.com/vercel/serve-handler#optionsreturn handler(request, response); })server.listen(3000, () => {console.log('Running at http://localhost:3000'); });

更多設置

幫助

shell> serve --helpserve - Static file serving and directory listingUSAGE$ serve --help$ serve --version$ serve folder_name$ serve [-l listen_uri [-l ...]] [directory]By default, serve will listen on 0.0.0.0:5000 and serve thecurrent working directory on that address.Specifying a single --listen argument will overwrite the default, not supplement it.OPTIONS--help Shows this help message-v, --version Displays the current version of serve-l, --listen listen_uri Specify a URI endpoint on which to listen (see below) -more than one may be specified to listen in multiple places-d, --debug Show debugging information-s, --single Rewrite all not-found requests to `index.html`-c, --config Specify custom path to `serve.json`-C, --cors Enable CORS, sets `Access-Control-Allow-Origin` to `*`-n, --no-clipboard Do not copy the local address to the clipboard-u, --no-compression Do not compress files--no-etag Send `Last-Modified` header instead of `ETag`-S, --symlinks Resolve symlinks instead of showing 404 errors--ssl-cert Optional path to an SSL/TLS certificate to serve with HTTPS--ssl-key Optional path to the SSL/TLS certificate's private keyENDPOINTSListen endpoints (specified by the --listen or -l options above) instruct serveto listen on one or more interfaces/ports, UNIX domain sockets, or Windows named pipes.For TCP ports on hostname "localhost":$ serve -l 1234For TCP (traditional host/port) endpoints:$ serve -l tcp://hostname:1234For UNIX domain socket endpoints:$ serve -l unix:/path/to/socket.sockFor Windows named pipe endpoints:$ serve -l pipe:\\.\pipe\PipeName

或者,參考官網介紹

參考

https://stackoverflow.com/questions/16333790/node-js-quick-file-server-static-files-over-http/24575241#24575241?newreg=f45f9198b20d4fc69d86e009ee614
https://www.cnblogs.com/leee/p/5502727.html

總結

以上是生活随笔為你收集整理的【Node.js】serve 实现目录浏览服务的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。