提供静态文件的nodejs(路径问题)(无法进入目录“ /../”)

时间:2018-08-09 22:11:48

标签: node.js express path

问题的简要概述

我位于routes.js文件中,如何将css文件夹中的所有文件作为静态文件提供?

path structure (someone edit this and make the img visible without use of link)


某些尝试失败

path.join(__dirname, '\\css')->输出-> \\app\\routes\\css

path.join(__dirname, '..\\css')->输出-> \\app\\routes\\css

path.join(__dirname, '..', 'css')->输出-> \\app\\routes\\css


所需的路径是:\\app\\routes\\..\\css

但是由于某些原因,“ /../”被忽略。 查看routes.js

中的评论

routes.js

var express = require("express");

var path = require('path');


var appRouter = function (app) {

  app.use(express.static(path.join(__dirname, '\\css')));
   // RESULTS IN INVALID PATH: '\\app\\routes\\css'

   // (there is no css folder inside the routes folder) 

  app.get('/', function (req, res) {
    res.redirect('/home');
      });
};

module.exports = appRouter;

server.js

var express = require("express");
var bodyParser = require("body-parser");
var routes = require("./routes/routes.js");
var app = express();

const server_port = process.env.PORT || 3000;

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

routes(app);

var server = app.listen(server_port);

Index.html

        // I have linked to the static css file
<link rel="stylesheet" type="text/css" href="css/styles.css" />

其他信息

app.use(express.static('app'));可以工作,但是它可以提供整个应用程序文件夹中的所有文件,这是不希望的。

1 个答案:

答案 0 :(得分:0)

使用

id

(您需要在目录名称之前添加uuid

相关问题