Home > Software design >  Can I see the code of .js files under api folder of NextJS with browsers?
Can I see the code of .js files under api folder of NextJS with browsers?

Time:03-27

I was wondering if I can see the code of ".js files" under "api folder" of NextJS with browsers.

I read the post Next.js API is back-end? then, one of the answers says:

The back-end or server-side of Next.js is with the api folder.

So, according to the answer above, it seems like api folder is for backend which means it's hidden from users or clients so I cannot see the code of ".js files" under "api folder" of NextJS with browsers.

But the documentation of NextJS about API Routes doesn't explicitly say "api folder is for backend" so is api folder really and exactly for backend which is hidden from users or clients? so I cannot see the code of ".js files" under "api folder" of NextJS with browsers?

CodePudding user response:

Yes, the API routes are only for the backend. If you ever wrote an Express server, or perhaps some PHP, you know that the end user can only see the rendered HTML/data. When a user visits an API route, the server-side JavaScript, which in the API folder is NOT isomorphic but rather only executing on the server side will only render the rendered response to be rendered.

If you want to show any JS in the client-side you know what you have to do—
Talk to public/.

Note: NextJS doesn't explicitly say it can't be viewed in the client side, but I would expect it to be somewhat obvious.

CodePudding user response:

Any file inside the folder pages/api is mapped to /api/* and will be treated as an API endpoint instead of a page. They are server-side only bundles and won't increase your client-side bundle size.

source

Which means they are not public available files after build process

  • Related