您好,我正在构建一个应用程序,使人们能够在工作板上发布工作。但是目前,如果发布了工作,它会直接转到主页/索引页面,而我不希望这样。我希望能够在作业板/主页/索引页上发布作业之前对其进行审查。有任何教程吗?
I.m当前正在使用有关Node的先前教程来构建该项目。
// Example code, but similar to actual code
//models config
let jobSchema = new mongoose.Schema({
title: String,
category: String,
description: String,
type: String,
url: String,
email: String,
apply: String,
location: String,
company: String,
path: String,
created: {type: Date, default: Date.now}
})
let Job = mongoose.model('job', jobSchema);
// {type: String, default: "placeholdeimage.jpg"}
//routes config
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './public/uploads/');
},
filename: function (req, file, cb) {
var originalname = file.originalname;
var extension = originalname.split(".");
filename = Date.now() + '.' + extension[extension.length-1];
cb(null, filename);
}
});
app.get('/', (req, res) =>{
res.redirect('/jobs')
})
app.get('/jobs', (req, res) =>{
Job.find({}).sort({ date: -1}).find(function(err, jobs){
if(err){
console.log('error')
}
return res.render('index', {jobs: jobs})
})
})
//add form route
app.get('/jobs/add', (req, res) => {
res.render('add')
})
app.post('/jobs', multer({storage: storage, dest:
'./public/uploads/'}).single('file'), (req, res) => {
req.body.description = req.sanitize(req.body.description);
Job.create( (err, addJob) =>{
if(req.file){
let fullPath = "uploads/"+req.file.filename;
let document = {
title: req.body.title,
category: req.body.category,
description: req.body.description,
type: req.body.type,
url: req.body.url,
email: req.body.email,
apply: req.body.apply,
location: req.body.location,
company: req.body.company,
path: fullPath
};
let job = new Job(document);
job.save()
}else{
console.log('file not uploaded')
logo = 'noimage.jpg'
}
//redirect to index page
return res.redirect('/jobs')
})
})
app.get('/jobs/:id', (req, res) => {
Job.findById(req.params.id, (err, jobDetails) => {
if(err){
res.redirect('/jobs')
}else{
res.render('details', {job: jobDetails});
}
})
})
app.listen(port, process.env.PORT, process.env.IP, ()=> console.log(`Server
is running on ${port}`))
<div class="container">
<% for (const job of jobs) { %>
<div class="row">
<div class="column logo" style="width:10%;">
<img src='<%= job.path %>'>
</div>
<div class="column title">
<h2>
<%=job.title%>
</h2>
<p>
<%=job.company%>
</p>
</div>
<div class="column type">
<h2>
<%=job.type%>
</h2>
</div>
<div class="column location">
<h2>
<%=job.location%>
</h2>
</div>
<div class="column">
<h2><a href="/jobs/<%= job._id %>"><button>Apply</button></a></h2>
</div>
</div>
<hr>
<% } %>
</div>
答案 0 :(得分:2)
您可以将字段approved: { type: Boolean, default: false }
添加到JobSchema
并检索有条件的作业。
Job.find({approved:true}).sort({ date: -1})...