I'm using Swagger to document my Node/Express API on my dev environment. It's working perfectly but now I need to disable it when going to production, in order to not let the API definition publicly reachable.
Is there a way to do it, using some npm script for example ?
Thanks
Keeping in line with convention, you wanna set NODE_ENV environment variable (environment variables are values set on OS, with no dependence to your app) to make things depend on the environment you're currently on. This'll heavily depend on where do you host your production app.
node app.js or npm run start (Or maybe you're using docker and your script ends with one of these commands.) In any case, before the execution of the "run application" command, make sure environment is set to production via export NODE_ENV=production command. You can check whether it worked via echo $NODE_ENV command.
Anyhow, once you're sure that NODE_ENV is production when the app is running in production, and with these assumptions:
With these assumptions, make it so that this is the first "app.use" type, middleware definition in your code:
if(process.env.NODE_ENV === "production"){
app.use('/docs', (req, res, next) => {
res.status(404).send("Not Found");
});
}
If any of the assumptions I've made does not pertain to your case, adjust them accordingly. And you're done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With