'Node js Cpanel go up one level path.join(__dirname , '..') not working
We are facing a issue that we can you create a folder in root directory in cpanel. I'm also sharing the code below
app.get('/preview',(req,res)=>{
const {userId} =req.session
if(userId)
{
pool.query("Select id,hostname,html from pages where id='"+req.session.userId+"'",function(err, result, fields){
if(err)
{
console.log(err)
}
else if(result=="")
{
res.send("<h1>No Preview available please make a portfolio first</h1>")
}
else if(result!="")
{
app.use(express.static(path.join(__dirname,'~',result[0].hostname+'.growupinfo.com')));
// res.render(path.join(__dirname,result[0].hostname+'.growupinfo.com','index.ejs'));
res.render(path.join(__dirname,'~',result[0].hostname+'.growupinfo.com','index.ejs'))
}
})
}
else
{
res.redirect("/login")
}
})
What we are trying to do is basically show a file that should be created in the root directory of cpanel through another code, (which also attempts to go back one level but doesnt work)
Error: Failed to lookup view "../../p.growupinfo.com/index.ejs" in views directory "./views"
at Function.render (/home/numltran/PortfolioBuilder/node_modules/express/lib/application.js:580:17)
at ServerResponse.render (/home/numltran/PortfolioBuilder/node_modules/express/lib/response.js:1017:7)
at Query.<anonymous> (/home/numltran/PortfolioBuilder/server.js:528:15)
at Query.<anonymous> (/home/numltran/PortfolioBuilder/node_modules/mysql/lib/Connection.js:526:10)
at Query._callback (/home/numltran/PortfolioBuilder/node_modules/mysql/lib/Connection.js:488:16)
at Query.Sequence.end (/home/numltran/PortfolioBuilder/node_modules/mysql/lib/protocol/sequences/Sequence.js:83:24)
at Query._handleFinalResultPacket (/home/numltran/PortfolioBuilder/node_modules/mysql/lib/protocol/sequences/Query.js:149:8)
at Query.EofPacket (/home/numltran/PortfolioBuilder/node_modules/mysql/lib/protocol/sequences/Query.js:133:8)
at Protocol._parsePacket (/home/numltran/PortfolioBuilder/node_modules/mysql/lib/protocol/Protocol.js:291:23)
at Parser._parsePacket (/home/numltran/PortfolioBuilder/node_modules/mysql/lib/protocol/Parser.js:433:10)
Solutions I have tried heres the error, as you can see i have tried ~, i have tried .., i have tried process.cwd() instead of dirname__ but no matter what the files created and are also in the folder of the app not one level up
sender.createdir = (id) =>{
console.log("Creating directory")
dir=path.join(process.cwd(),"..",id+".growupinfo.com")
console.log("ID:"+id)
console.log(dir)
fs.mkdir(dir,{recursive:true}, (error) => {
if(error){
console.error('This error occured'+error)
}
else
{
console.log('The directory for ID: '+id)
}
})
};
this code is used to create the folder for reference and as you can see i have used .. and process.cwd()
What i mean by one level up is if the app is in folder /example/app/ then i want this code to go to /example/
Thank you (PS it works exactly as intended in localhost)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
