Reputation: 119
I'm setting up an admin panel, and the admin enters his custom url in a textarea that stores in the db by clicking enter
. Now I want to redirect him to the custom url that he entered already. How is that possible?
In the below code how can I declare the customURL
from database?
router.get('/blog/'+customURL, (req, res) => {
MongoClient.connect(url, function(err, db) {
if (err) throw err
var dbo = db.db("barg")
var query = { username: "a" }
dbo.collection("post").find(query).toArray(function(err, result) {
if (err) throw err
console.log(result[0].url)
res.render('blog',{
post : result[0].content
})
db.close()
})
})
})
Upvotes: 0
Views: 43
Reputation: 40444
What you have to use is a path parameter, and check if the path provided is a valid URL. If it's not, you end the request with: 404 - Not found
router.get('/blog/:path', async(req, res) => {
// check if req.params.path is a valid URL in DB
const validUrl = await isValidUrl(req.params.path);
if(!validUrl) {
return res.status(404).send('not found');
}
MongoClient.connect(url, function(err, db) {
if (err) throw err
var dbo = db.db("barg")
var query = { username: "a" }
dbo.collection("post").find(query).toArray(function(err, result) {
if (err) throw err
console.log(result[0].url)
res.render('blog',{
post : result[0].content
})
db.close()
})
})
})
Upvotes: 1