【Node.js】serve 实现目录浏览服务
生活随笔
收集整理的這篇文章主要介紹了
【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 实现目录浏览服务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大的来了!曝华为麒麟芯片已经换代 AI算
- 下一篇: 氮化镓快充市场增长迅猛 倍思用技术创新