Search code examples
javascriptnode.jsmongodbnext.js

Next.js with API routes vs. Express.js API


How does Next.js with API routes differ technically from running an Node.js server with Express.js?

For example, if I want to develop a full-stack web application with MongoDB, can I use only Next.js with API routes for that purpose? Is connecting and modifying the database safe from the API routes, or is it a security risk?

In other words, are the Next.js API routes exposed into the client browser where the end user would be able to modify the code?


Solution

  • Next.js API routes are ran on server side. Per the docs:

    They are server-side only bundles and won't increase your client-side bundle size

    So the end user is unable to modify the code. Just make sure they are in the /pages/api folder. Read the docs for more info: https://nextjs.org/docs/api-routes/introduction