繁体   English   中英

解决NodeJS / Express中的相对路径

[英]Resolve relative paths in NodeJS/Express

我有一个网站在不同的文件夹(主文件夹的内部和外部)中调用多个脚本,图像,样式等:

文件树

- /
  - website
      - scripts
        - data.js
        - customJquery.js
      - styles
        - animate.css
      index.html
      main.css
      back.jpg
  - otherFunctions.js

的index.html

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Montserrat:400,400i,800,800i">
        <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Faster+One">
        <link rel="stylesheet" href="styles/animate.css">
        <link rel="stylesheet" href="main.css">
    </head>
    <body>
        <img class="fondo" src="back.jpg">

        <div class="content">
            <!-- stuff... -->
        </div>

        <script src='scripts/data.js'></script>
        <script src='scripts/customJquery.js'></script>
        <script src='../otherFunctions.js'></script> <!-- Here's the conflict... -->
    </body>
</html>

除了../otherFunctions.js之外,所有路径都可以正常路由。 似乎NodeJS / Express跳过了相对的部分..而仅接收到/otherFunctions.js ,该错误处理了。

这是我的服务器端:

index.js

const express = require('express');
const https = require('https');
const fs = require('fs');
const app = express();

const config = require('./config');

var webId;

var options = {
  key: fs.readFileSync(config.paths.certificate.key),
  cert: fs.readFileSync(config.paths.certificate.crt),
  requestCert: false,
  rejectUnauthorized: false
};

app.use(function (req, res, next) {
  res.setHeader("Access-Control-Allow-Origin", "*");
  res.setHeader("Access-Control-Allow-Credentials", "true");
  res.setHeader("Access-Control-Allow-Methods", "GET, HEAD, OPTIONS, POST, PUT");
  res.setHeader("Access-Control-Allow-Headers", "Accept, Access-Control-Allow-Headers, Access-Control-Request-Headers, Access-Control-Request-Method, Authorization, Content-Type, Origin, X-Requested-With");
  next();
});

app.get('/favicon.ico', function(req, res) {
  res.status(404).send('No favicon found');
});

app.get('/:id', function(req, res, next) {
  id = req.params.id;

  if (id.search(/\w+\.[A-z]+$/g) < 0) {
    webId = id;
    res.sendFile('index.html', {root: config.paths.webs + id});
  } else {
    res.sendFile(id, {root: config.paths.webs});
  }
});

app.get('/:folder/:file', function(req, res) {
  let folder = req.params.folder;
  let file = req.params.file;

  res.sendFile(file, {root: config.paths.webs + webId + '/' + folder});
});


app.get('*', (request, response) => {
  response.send('GET request not found: ' + request.url);
});


app.use((err, request, response, next) => {
  response.status(500).send(err.message);
});

https.createServer(options, app).listen(443, function() {
  console.clear();
  console.log("NodeJS secure server started at port 443");
});

除非您特别需要通过这种方式提供静态文件,否​​则建议您使用Express内置的静态文件服务

关于在HTML中使用“ ../otherFunctions.js”,恐怕浏览器会尝试解析相对于HTML文件本身位置的路径,例如,如果HTML文件是my.domain.com/foo/bar/index.html ,浏览器将寻找my.domain.com/foo/otherFunctions.js 理想情况下,您要提供给客户端的所有静态文件应放在一个文件夹中,然后由express.static(...)调用使用。

暂无
暂无

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

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