简体   繁体   English

从Node.js提供静态文件

[英]Serving static files from Node.js

I'm trying to serve static files from Node.js the only problem I'm having, is if I keep going into sub paths, like so: 我试图从Node.js提供静态文件的唯一问题是,如果我继续进入子路径,就像这样:

localhost:3000/foo/bar/baz/quux

Then I have to step up the same amount of times, like this: 然后,我必须加倍执行相同的操作,如下所示:

../../../../public/javascripts/whatever.js

As you can see that gets really annoying, is there a way to make Express v3 just know so that I can just do /public/javascripts/whatever.js instead of having to step up? 如您所见,这真的很烦人,有没有办法让Express v3知道,这样我就可以执行/public/javascripts/whatever.js而不用/public/javascripts/whatever.js步伐了? Thanks in advance 提前致谢

This is my current static middleware for Express` 这是我目前用于Express的静态中间件。

app.use("/public", express.static(__dirname + '/public'));

If you reference your static files from the root (ie src='/some/path/to/file.js' ), the url should not matter. 如果您从根目录引用静态文件(即src='/some/path/to/file.js' ),则该URL应该无关紧要。

Example Website using Static Routing 使用静态路由的示例网站

Directory Structure 目录结构

/public
    /css/style.css
    /js/site.js
/vendor/thoughtbrain/js/awesome-town.js
/views/view.html
/app.js

view.html view.html


<!DOCTYPE html>
<html>
  <head>
    <!-- These files are served statically from the '/public' directory... -->
    <link href="/css/style.css" rel="stylesheet" >
    <script src="/js/site.js"></script>
    <!-- ... while this is "mounted" in virtual '/public' -->
    <script src="/public/js/awesome-town.js"></script>
  </head>
<body><p>Express</p></body>
</html>

app.js app.js

var express = require('express'),
    http = require('http'),
    path = require('path'),
    app = express();

// Remember: The order of the middleware matters!

// Everything in public will be accessible from '/'
app.use(express.static(path.join(__dirname, 'public')));

// Everything in 'vendor/thoughtbrain' will be "mounted" in '/public'
app.use('/public', express.static(path.join(__dirname, 'vendor/thoughtbrain')));

app.use(express.static(path.join(__dirname, 'views')));

app.all('*', function(req, res){
  res.sendfile('views/view.html')
});

http.createServer(app).listen(3000);

With this application running, 有了这个应用程式,

http://localhost:3000

and

http://localhost:3000/foo/bar/baz/quux

both serve view.html and all referenced assets resolve. 都投放view.html ,所有引用的资产都解析。

Express Framework has a section on the use of static middleware here . Express Framework在此处介绍了如何使用静态中间件

With that static() configuration, Express is already at least capable of finding /public/javascripts/whatever.js . 使用static()配置,Express至少已经能够找到/public/javascripts/whatever.js

Though, it does depend on whether your public folder is in the same directory as your script (due to the use of __dirname when specifying the path). 虽然,它确实取决于你是否public (由于使用的文件夹是在同一目录作为脚本__dirname指定路径时)。

If it is, the URL prefix /public should map to the file-system prefix of ./public (with . being __dirname ) so that: 如果是,URL前缀/public应该映射到./public的文件系统前缀( .__dirname ),以便:

A URL of `/public/javascripts/whatever.js`
Maps to `./public/javascripts/whatever.js`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM