Skip to content
Search
  • ServicesPage
  • Our workPage
  • Case studiesPage
  • BlogPage
  • AI solutionsPage
  • Contact usPage
All articles

Moving a Laravel API to NestJS without stopping the product

Moinul Hossain Miraj · · 4 min read · Web Development

Moving a Laravel API to NestJS without stopping the product

Rewriting a working API in another framework is risky. Customers do not care which framework runs the backend; they care that nothing breaks. When we moved a Laravel API to NestJS, the goal was simple: same behaviour, same data, no big-bang switch.

Here is how we approached it, and the differences between the two frameworks that mattered most.

Why NestJS

The front end was already TypeScript. Moving the API to NestJS meant one language across the stack, shared types between front end and back end, and one set of tools. NestJS also feels familiar to Laravel developers: it has modules, dependency injection, controllers, middleware and a clear structure, so the team was not starting from zero.

Mapping Laravel ideas to NestJS

Most Laravel concepts have a direct NestJS counterpart (Laravel on the left, NestJS on the right). Writing this mapping first made the rest of the work predictable.

  • Routes and controllers → Controllers with decorators (@Get, @Post)
  • Service providers and the container → Modules and dependency injection
  • Form Requests (validation) → DTO classes with class-validator, and a global ValidationPipe
  • Middleware → Middleware, Guards (auth) and Interceptors (logging, response shape)
  • Eloquent models → TypeORM or Prisma entities
  • Migrations → ORM migrations (we kept the existing schema, see below)
  • Queued jobs → BullMQ queues and processors
  • Scheduler (schedule:run) → @nestjs/schedule cron jobs
  • Sanctum / Passport auth → Passport strategies with JWT
  • API Resources → Response DTOs or interceptors that shape the output

Keep the database, move the code

The most important decision: do not change the database at the same time. The NestJS app connected to the same database schema the Laravel app used. That meant:

  • No data migration day.
  • Both apps could run side by side and read the same data.
  • If a NestJS endpoint misbehaved, the Laravel one was still there.

Schema improvements came later, as separate, small changes.

Move route by route

Instead of switching everything at once, we moved one group of endpoints at a time behind the same public address. A proxy in front sent the moved routes to NestJS and everything else to Laravel. Each group went through the same steps:

  1. Write the NestJS version against the same database.
  2. Compare: call the old and new endpoint with the same requests and compare the responses field by field.
  3. Switch the route to NestJS.
  4. Watch errors and response times, keeping the Laravel route ready to switch back.

When the last route moved, Laravel was simply turned off.

The small differences that cause the real bugs

The framework mapping is the easy part. These details are where the bugs hide.

Password hashes. Laravel stores bcrypt hashes with a $2y$ prefix. Some Node bcrypt libraries only accept $2a$ or $2b$. The algorithm is the same, so replacing the prefix before comparing lets existing users keep their passwords:

js
const hash = stored.replace(/^\$2y\$/, "$2b$");
const ok = await bcrypt.compare(password, hash);

Decimals. MySQL DECIMAL columns often come back from Node drivers as strings, not numbers, to avoid rounding errors. Laravel casts them for you. In NestJS you decide: keep them as strings for money and convert carefully, or use a decimal library. Never let them turn into floating-point numbers by accident.

Dates and time zones. Laravel's Carbon and the app timezone setting do a lot quietly. In Node, set the database connection and the server to UTC, store UTC, and convert only when showing dates. Compare a few date fields between the old and new responses explicitly.

Validation messages and error shapes. Front ends often depend on Laravel's 422 responses with an errors object per field. NestJS's ValidationPipe returns a different shape by default. We added an exception filter so the new API returned the same error format, and the front end did not need to change.

Empty values. PHP treats null, empty strings and missing keys differently from JavaScript in places. Compare responses for records with empty fields, not just complete ones.

Background jobs. A Laravel job that retries automatically needs its retry and backoff settings set explicitly in BullMQ, and jobs must be safe to run twice.

What made it go smoothly

  • One rule: same database, same responses, one route group at a time.
  • Automated comparison of old and new responses, not manual clicking.
  • A way to switch any route back in minutes.
  • Shared TypeScript types between the API and the front end once the move was done.

We use AI coding tools to speed up work like this, translating controllers and writing comparison tests, and an engineer reviews every change before it ships. That combination suits a migration well: lots of repetitive translation, where a careful review catches the small differences above.

If you are thinking about moving a Laravel or PHP backend to NestJS, talk to us.

Have a project in mind?

Tell us what you need. We get back to you as soon as possible.

Start a project