最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - Expressjs how to distribute static file loading depending on the path in the address bar - Stack Overflow

programmeradmin1浏览0评论

There is such a simple server

import express from "express";
import path from "path";
import fs from "fs";

const __dirname = path.resolve();
const app = express();

app.use(express.json());

app.use(express.static(path.join(__dirname, `src`), { index: 'none' }));///////////////

app.use(express.static(path.join(__dirname, `admin`), { index: 'none' }));////////////


app.get(`/*`, (req, res) => {
    let mainHtmlPath;
    if (req.url === '/admin') {  ///  // random error
        mainHtmlPath = path.join(__dirname, `admin`, 'index.html');
    } else {
        mainHtmlPath = path.join(__dirname, `src`, 'index.html');
    }

    fs.readFile(mainHtmlPath, 'utf8', (err, html) => {
        if (err) console.log(err, 'Ошибка')
        res.send(html)
    })
});


app.listen(80, () => console.log(`Server is run on port 80`))

I can't figure out how to make expressjs load the right static files

In the end, I need it to be like this

if the path starts with just /, so that static files are loaded only from the src folder especially since it can change, for example /user/setting/

and of course

if the path starts with just /admin, so that static files are loaded only from the admin folder especially since it can change, for example admin/users/list/

I don't know if I should give an example of my meager attempts

one of the variants of my attempts

app.use((req, res, next) => {
    console.log('++++++++++++++++++++++++++++++++++++++++++', req.url === '/admin/');
    console.log('++++++++++++++++++++++++++++++++++++++++++', req.url);
    if (req.url[0] === '/admin/') {
        express.static(path.join(__dirname, `admin`), { index: 'none' })
    } else {
        express.static(path.join(__dirname, `src`), { index: 'none' })
    }
    next()
})

I get this response in the console

but still, static files are not loading.

There is such a simple server

import express from "express";
import path from "path";
import fs from "fs";

const __dirname = path.resolve();
const app = express();

app.use(express.json());

app.use(express.static(path.join(__dirname, `src`), { index: 'none' }));///////////////

app.use(express.static(path.join(__dirname, `admin`), { index: 'none' }));////////////


app.get(`/*`, (req, res) => {
    let mainHtmlPath;
    if (req.url === '/admin') {  ///  // random error
        mainHtmlPath = path.join(__dirname, `admin`, 'index.html');
    } else {
        mainHtmlPath = path.join(__dirname, `src`, 'index.html');
    }

    fs.readFile(mainHtmlPath, 'utf8', (err, html) => {
        if (err) console.log(err, 'Ошибка')
        res.send(html)
    })
});


app.listen(80, () => console.log(`Server is run on port 80`))

I can't figure out how to make expressjs load the right static files

In the end, I need it to be like this

if the path starts with just /, so that static files are loaded only from the src folder especially since it can change, for example /user/setting/

and of course

if the path starts with just /admin, so that static files are loaded only from the admin folder especially since it can change, for example admin/users/list/

I don't know if I should give an example of my meager attempts

one of the variants of my attempts

app.use((req, res, next) => {
    console.log('++++++++++++++++++++++++++++++++++++++++++', req.url === '/admin/');
    console.log('++++++++++++++++++++++++++++++++++++++++++', req.url);
    if (req.url[0] === '/admin/') {
        express.static(path.join(__dirname, `admin`), { index: 'none' })
    } else {
        express.static(path.join(__dirname, `src`), { index: 'none' })
    }
    next()
})

I get this response in the console

but still, static files are not loading.

Share edited Feb 16 at 11:53 Mureinik 312k54 gold badges358 silver badges391 bronze badges asked Feb 11 at 10:35 AirAir 2034 silver badges17 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

You can apply the static middleware on a specific path:

app.use('/', express.static(path.join(__dirname, `src`), { index: 'none' }));
// Here-^

app.use('/admin', express.static(path.join(__dirname, `admin`), { index: 'none' }));
// Here-^

in your console.log you check if req.url === '/admin/ but in your condition you check req.url[0] and not req.url so you remain falling in the else ;)

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论