企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# nodejs中resolve()在url中的使用方法 ``` <pre class="calibre29">``` <span class="token2">var</span> url <span class="token">=</span> <span class="token3">require</span><span class="token1">(</span><span class="token4">'url'</span><span class="token1">)</span><span class="token1">;</span> <span class="token2">var</span> a <span class="token">=</span> url<span class="token1">.</span><span class="token3">resolve</span><span class="token1">(</span><span class="token4">'/one/two/three'</span><span class="token1">,</span> <span class="token4">'four'</span><span class="token1">)</span> <span class="token1">,</span> b <span class="token">=</span> url<span class="token1">.</span><span class="token3">resolve</span><span class="token1">(</span><span class="token4">'http://example.com/'</span><span class="token1">,</span> <span class="token4">'/one'</span><span class="token1">)</span><span class="token1">,</span> c <span class="token">=</span> url<span class="token1">.</span><span class="token3">resolve</span><span class="token1">(</span><span class="token4">'http://example.com/one'</span><span class="token1">,</span> <span class="token4">'/two'</span><span class="token1">)</span><span class="token1">;</span> console<span class="token1">.</span><span class="token3">log</span><span class="token1">(</span>a <span class="token">+</span><span class="token4">","</span><span class="token">+</span> b <span class="token">+</span><span class="token4">","</span><span class="token">+</span> c<span class="token1">)</span><span class="token1">;</span> <span class="token6">//输出结果:</span> <span class="token6">///one/two/four</span> <span class="token6">//http://example.com/one</span> <span class="token6">//http://example.com/two</span> ``` ``` ``` <pre class="calibre14">``` <span class="token2">const</span> global <span class="token">=</span> <span class="token3">require</span><span class="token1">(</span><span class="token4">"../common/global"</span><span class="token1">)</span><span class="token1">;</span> <span class="token2">const</span> path <span class="token">=</span> <span class="token3">require</span><span class="token1">(</span><span class="token4">"path"</span><span class="token1">)</span><span class="token1">;</span> <span class="token2">const</span> fs <span class="token">=</span> <span class="token3">require</span><span class="token1">(</span><span class="token4">"fs"</span><span class="token1">)</span><span class="token1">;</span> <span class="token2">const</span> express <span class="token">=</span> <span class="token3">require</span><span class="token1">(</span><span class="token4">"express"</span><span class="token1">)</span><span class="token1">;</span> <span class="token2">const</span> router <span class="token">=</span> express<span class="token1">.</span><span class="token3">Router</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> router<span class="token1">.</span><span class="token2">get</span><span class="token1">(</span><span class="token4">"/:fileName"</span><span class="token1">,</span> <span class="token2">async</span> <span class="token1">(</span>req<span class="token1">,</span> res<span class="token1">,</span> next<span class="token1">)</span> <span class="token">=></span> <span class="token1">{</span> console<span class="token1">.</span><span class="token3">log</span><span class="token1">(</span>fileName<span class="token1">)</span> <span class="token2">let</span> <span class="token1">{</span> fileName <span class="token1">}</span> <span class="token">=</span> req<span class="token1">.</span>params<span class="token1">;</span> <span class="token2">let</span> uploadPath <span class="token">=</span> global<span class="token1">.</span><span class="token5">UPLOAD_PATH</span><span class="token1">;</span> <span class="token2">let</span> filePath <span class="token">=</span> path<span class="token1">.</span><span class="token3">resolve</span><span class="token1">(</span>uploadPath<span class="token1">,</span> fileName<span class="token1">)</span><span class="token1">;</span> fs<span class="token1">.</span><span class="token3">createReadStream</span><span class="token1">(</span>filePath<span class="token1">)</span><span class="token1">.</span><span class="token3">pipe</span><span class="token1">(</span>res<span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span><span class="token1">)</span><span class="token1">;</span> module<span class="token1">.</span>exports <span class="token">=</span> router ``` ```