每当我尝试使用node.js
将form.js
文件(res.redirect("/");
)重定向到同一页面时,它都起作用!
但是,当我尝试使用welcome.ejs
重定向到同一文件夹中的另一个文件(res.redirect("/welcome");
)时,它将无法正常工作。
我的welcome.ejs
文件中<h1>Welcome Home</h1>
就是这个句子。
我的mysql数据库工作正常,我可以看到消息“受影响的行”。所以我很确定那里没有问题。 我什至没有任何错误。
我不明白为什么我不能重定向到welcome.ejs
页,但是我可以重定向到主页(home.ejs
或"/"
)。在浏览器中,消息显示“无法获得/欢迎”
我什至尝试返回res.redirect(/welcome)
或res.redirect("/welcome.ejs")
,但没有成功!
我的文件夹结构为views/home.ejs
和views/welcome.ejs
。
有人可以帮我吗?
这是我的form.js
代码
app.get('/accountform', function(req, res) {
var q = "SELECT COUNT(*) AS count FROM `Projects`";
connection.query(q, function(err, results){
if (err) throw err;
var count = results[0].count;
//res.send("We have " + count + " no of users.");
res.render("accountform", {count: count});
});
app.post('/addproject', function(req, res){
var project = {
Project_No: req.body.Project_No,
Project_Name: req.body.Project_Name,
Status: req.body.Status,
Project_Type: req.body.Project_Type
};
connection.query('insert into `Projects` SET ?', project, function(err,
result){
console.log(err);
console.log(result);
res.redirect("/welcome");
});
});
});
答案 0 :(得分:0)
我在您的代码附近尝试了一些
这是我的app.js
const {getHomePage} = require('./routes/index');
const {addPlayer} = require('./routes/player');
app.get('/', getHomePage);
app.post('/add', addPlayer);
routes / index.js
module.exports = {
getHomePage: (req, res) => {
let query = "SELECT * FROM `players` ORDER BY id ASC";
db.query(query, (err, result) => {
if (err) {
res.redirect('/');
}
res.render('index.ejs', {
title: "Welcome to Socka | View Players"
,players: result
});
});
},
};
routes / player.js
module.exports = {
addPlayerPage: (req, res) => {
res.render('add-player.ejs', {
title: "Welcome to Socka | Add a new player"
,message: ''
});
},
addPlayer: (req, res) => {
if (!req.files) {
return res.status(400).send("No files were uploaded.");
}
let message = '';
let first_name = req.body.first_name;
let last_name = req.body.last_name;
let position = req.body.position;
let number = req.body.number;
let username = req.body.username;
let uploadedFile = req.files.image;
let image_name = uploadedFile.name;
let fileExtension = uploadedFile.mimetype.split('/')[1];
image_name = username + '.' + fileExtension;
let usernameQuery = "SELECT * FROM `players` WHERE user_name = '" + username +
"'";
db.query(usernameQuery, (err, result) => {
if (err) {
return res.status(500).send(err);
}
if (result.length > 0) {
message = 'Username already exists';
res.render('add-player.ejs', {
message,
title: "Welcome to Socka | Add a new player"
});
} else {
if (uploadedFile.mimetype === 'image/png' || uploadedFile.mimetype ===
'image/jpeg' || uploadedFile.mimetype === 'image/gif') {
uploadedFile.mv(`public/assets/img/${image_name}`, (err ) => {
if (err) {
return res.status(500).send(err);
}
let query = "INSERT INTO `players` (first_name, last_name,
position, number, image, user_name) VALUES ('" +
first_name + "', '" + last_name + "', '" + position + "', '"
+ number + "', '" + image_name + "', '" + username + "')";
db.query(query, (err, result) => {
if (err) {
return res.status(500).send(err);
}
res.redirect('/');
});
});
} else {
message = "Invalid File format. Only 'gif', 'jpeg' and 'png' images
are allowed.";
res.render('add-player.ejs', {
message,
title: "Welcome to Socka | Add a new player"
});
}
}
});
}
}
我在views文件夹中有index.ejs和addplayer.ejs
这是我完整的github代码 https://github.com/gayathribrittany/nodeapp