/users/mihir/users/[object%20Object]/file.txt
我有以下 pug 文件和 JS 代码来渲染 pug 页面。我在路径 filepath 中有目录和文件的链接,为此我在实际目录名或文件名之前添加“users/”加上用户名。但是,在 pug 中使用标签显示的文件或目录的链接具有如上所示的文件路径。我不知道出了什么问题。
subdir.pug
html
head
title= title
body
h1= title
hr
ul
li
a(href='../') Go Back
each val in listing
li
a(href='users/' + user + '/' +val) !{val}
hr
res.render('subdir', { title: title + req.path, listing: fs.readdirSync(filepath), user: username })
完整的JS文件:
const e = require('express');
const express = require('express');
const app = express();
app.use(express.static('css'));
const fs = require('fs');
const path = require("path")
const port = 3000;
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static('public'));
let users = require('./users.json');
const userIDPattern = Object.keys(users).join('|');
app.use('/', express.static('assets'));
app.set('view engine', 'pug')
const title = `Directory Listing for ${__dirname}`
/*
app.get('/', (req, res) => {
listing = fs.readdirSync(__dirname)
res.render('index', { title: title + '/', listing: listing })
})
app.get('/users/*', (req, res) => {
console.log(userIDPattern)
console.log(req.path)
let user = req.query.user;
const filePath = path.join(__dirname, req.path)
console.log(filePath)
if (fs.existsSync(filePath)) {
const lstat = fs.lstatSync(filePath)
if (lstat.isDirectory()) {
listing = fs.readdirSync(filePath)
res.render('subdir', { title: title + req.path, listing: listing })
} else {
res.sendFile(filePath)
}
res.render('index', { title: title + req.path })
}
else {
res.status(404).render("404", { file: filePath })
}
})
*/
app.get('/', (req, res) => {
console.log("Currently in the login page")
res.render('login');
});
/*
app.get(new RegExp(`/users/(${userIDPattern})`), (req, res) => {
console.log("Currently in the user page")
const username = req.params[0]; // Extract the matched ID from the URL
console.log(username);
const filePath = path.join(__dirname, req.path)
listing = fs.readdirSync(filePath)
res.render('index', { title: title + req.path, listing: listing })
});
app.get(new RegExp(`/users/(${userIDPattern})/.*`), (req, res) => {
const username = req.params[0]; // Extract the matched ID from the URL
});
*/
app.get('/index', (req, res) => {
console.log("Currently in the index page")
const username = req.query.user;
console.log(username);
console.log(req.path)
const filePath = path.join(__dirname, "users/" + username)
console.log(filePath)
listing = fs.readdirSync(filePath)
res.render('index', { title: title + '/users/' + username, listing: listing, user: username })
})
app.get(/^\/users\/[a-zA-Z0-9_-]+\/.*$/, (req, res) => {
const username = req.body;
// Handle requests matching the regex pattern
//console.log(req.path)
//console.log(username)
const filepath = path.join(__dirname, req.path)
console.log("PATH: " + req.path)
const stats = fs.statSync(filepath);
console.log(stats.isDirectory());
if (stats.isDirectory()) {
res.render('subdir', { title: title + req.path, listing: fs.readdirSync(filepath), user: username })
} else {
console.log("Request to view a file")
res.sendFile(filepath)
}
//res.send('Matching the pattern: /users/someUser/someFileOrDirectory');
});
app.post('/submit', (req, res) => {
console.log(req.body);
if (req.body.submit == "Submit") {
const username = req.body.username
const password = req.body.password
if (username in users) {
if (users[username] == password) {
res.redirect('/index' + "?user=" + username)
} else {
res.render('error', { message: "Invalid username or password!" })
}
}
} else {
console.log("We have come to signup")
res.render('signup')
}
});
app.post("/signup", (req, res) => {
res.render('signup')
});
app.post("/signupsubmit", (req, res) => {
console.log(req.body);
const accept = req.body.check === "on"
if (accept) {
console.log("Accepted terms and conditions!")
const user = {
username: req.body.username,
password: req.body.password
}
if (user.username == "" && user.password == "") {
res.render('error', { message: "You must enter a username and password!" })
}
console.log(user.username in users)
if (user.username in users) {
res.render('error', { message: "That username is already taken!" })
}
console.log(users)
users[user.username] = user.password
console.log(users)
fs.writeFileSync('users.json', JSON.stringify(users), (err) => {
if (err) throw err;
else {
console.log("The file is updated with the given data")
}
});
const directoryPath = 'users/' + user.username;
if (fs.existsSync(directoryPath)) {
res.redirect('/index' + "?user=" + user.username)
} else {
fs.mkdirSync(directoryPath)
res.redirect('/index' + "?user=" + user.username)
}
} else {
res.render('error', { message: "You must accept the terms and conditions!" })
//res.redirect('/') // this redirects to the login page
}
});
app.get("/users/*", (req, res) => {
})
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
我对index.pug页面做了同样的事情,如下所示,并且它在那里工作得非常好。
html
head
title= title
link(href= './styles.css', rel='stylesheet', type='text/css')
body
h1= title
hr
ul
each val in listing
li
a(href='users/' + user + '/' +val) !{val}
hr
鉴于您在 JS 代码中引用了
user.username
,想必您会想要
a(href='users/' + user.username + '/' +val) !{val}
也在你的 Pug 代码中(因为你现在显然正在获得一个对象作为你的
user
)。