admin管理员组

文章数量:1292646

This is a server

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

import adminRoutes from './routes/adminRoutes.js';
import clientRoutes from './routes/clientRoutes.js';

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

app.use(express.json());

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

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

./routes/adminRoutes.js && ./routes/clientRoutes.js

import express from "express";
import path from "path";
import fs from "fs";
const router = express.Router();
const app = express()
const __dirname = path.resolve();
router.get('/*', (req, res) => {
    console.log('============================', req.url);
    fs.readFile(path.join(__dirname, `src`, 'index.html'), 'utf8', (err, html) => {
        if (err) console.log(err, 'Error')
        res.send(html)
    })
});

export default router;


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

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

router.get('/admin/', (req, res) => {
    console.log('============================', req.url);

    fs.readFile(path.join(__dirname, `admin`, 'index.html'), 'utf8', (err, html) => {
        if (err) console.log(err, 'Error')
        res.send(html)
    })
});

export default router;

It's difficult to formulate a question when you don't understand the essence of the problem.

I want to understand how it works and how to make it so that if the path in the address bar does not start with admin

For example

http://localhost/

http://localhost/ee/rewr

http://localhost/ee/rewr/rrrrr

then the server should read only the src folder

and if the path starts with admin

for example

http://localhost/admin

http://localhost/admin/ee/rewr

http://localhost/admin/ee/rewr/rrrrr

then the server should read only the admin folder

here is the whole structure of the project

if in the file clientRoutes.js in the line router.get('/', (req, res) => { add /* router.get('/*', (req, res) => {

server reads css but without js but if you reload the page everything works

even if you add some transition to the path http://localhost/sss

but if the path is more difficult http://localhost/sss/5555

everything is falling apart

and as for the admin, nothing works at all

naturally I understand that * in the clientRoutes.js file

router.get('/*', (req, res) => {

router.get('/', (req, res) => {

the admin is interfering with loading and I am removing it

and in the end I get this

Can someone explain to me how I can implement all this correctly and competently?

There was also this server option

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('/admin', express.static(path.join(__dirname, `admin`), { index: 'none' }));

app.get(`/*`, (req, res) => {
    let mainHtmlPath;
    if (req.url[0] === '/admin') {
        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, 'Error')
        res.send(html)
    })
});

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

that's the difference. and the rest is all described above in the question

本文标签: nodejsHow to correctly and efficiently set up transitions between pages on a websiteStack Overflow