I have a folder
api-docs inside that I have index.html some css and js file
I need to render api-doc for authenticated user.
I am not using it in views as In project I am using jade in view and api-doc is in html
I have tried
router.get('/v1/secure-api-documentation',(req,res)=>{
console.log('A')
res.sendFile(__dirname + '/../api-doc/index.html');
});
and
router.get('/v1/secure-api-documentation',ensureAuthenticate,(req,res)=>{
express.static(path.join(__dirname,'../api-doc'))
});
express.static(path, [options]) returns a function. So basically what your code is doing is :
router.get('/v1/secure-api-documentation',ensureAuthenticate,(req,res)=>{
express_static_function // this function further accepts arguments req, res, next
//there is no function call happening here, so this is basically useless
});
However, this is not what express.static is used for What express.static does is, takes the request path and looks for a file with the same name in the folder you specified.
Basically, if a GET request comes to '/v1/secure-api-documentation' , it will take the request path after '/v1/secure-api-documentation' and look for that inside api_docs folder. Passing express.static to router.get() will call it for the very SPECIFIC path. This is important. GET '/v1/secure-api-documentation/index.html' will fail. Because such a route is not handled.
What you need to do this is call express static for any path like '/v1/secure-api-documentation/*' .
For this you need to take the express app object, and write the following code:
//make sure to use the change the second argument of path.join based on the file where your express app object is in.
app.use('/v1/secure-api-documentation',express.static(path.join(__dirname,'../api-doc')));
This will now work for not only the index.html file but any js/css file inside api_docs that is requested.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.