节点expressionapp.get(“/ ..”父目录不工作

  1. ./core/server.js
  2. ./core/index.html
  3. ./core/styles.css
  4. ./bin/init-transpiled.js

如何在localhost:8000 (not /core )上托pipeindex.html ,其中index.html仍然可以访问相对path../bin/init-transpiled./styles.css

你可以像这样托pipe你的index.html文件。

 app.get('/', function(req, res) { res.sendFile(path.resolve(__dirname + "/index.html")); }); 

而且您需要将bin文件夹设置为服务器configuration中的静态文件夹,以便从客户端访问bin文件夹文件。

 app.use(express.static(path.resolve(__dirname + '/../bin'))); 

然后在index.html中,你可以像这样访问你的文件。

 <script src="../bin/init-transpiled.js"> 

请注意,如果index例如使用./styles.css ,则需要使用../core/styles.css

 app.use('/', express.static(__dirname)); app.use('/', express.static(__dirname + '/../')); app.get('/', function (req, res) { res.sendFile(__dirname + "/index.html"); }); 

当使用'/'

  1. 静态托pipe这个目录(例如访问本地的./styles.css
  2. 静态宿主父目录(否则你不能遍历../bin/
  3. 发送index.html作为回应。

注意: '/'不是必需的,默认是'/' ,你可以把你的中间件处理函数作为第一个参数

请参阅快速文档 – 静态文件