Basic differences between Laravel and Express.js

Posted on

If you are previously familiar with Laravel and want to migrate to Express JS, then there are some important basics that you must learn.

The Most Important Thing You Should Learn

Knowledge of Laravel architecture (MVC), routing, and middleware is invaluable. However, there are some fundamentally new concepts in the world of Node.js/Express.

1. Asynchronous JavaScript (Async/Await and Promises)

This is the biggest and most important technical difference from PHP.

  • In PHP (Laravel), code runs synchronously (one at a time from top to bottom)
  • In Node.js, operations like database queries or API calls are asynchronous (non-blocking). The code doesn’t wait for those operations to complete.

You need to master async/await to handle this.

Example in Laravel (Synchronous):

// The code waits until the user is found, then continues to the next line.
$user = User::find(1);
return view('profile', ['user' => $user]);

Example in Express (Asynchronous):

// 'async' marks this function as asynchronous
app.get('/users/:id', async (req, res) => {
  try {
    // 'await' pauses the function UNTIL the user data is obtained
    const user = await prisma.user.findUnique({ where: { id: Number(req.params.id) } });
    res.json(user);
  } catch (error) {
    res.status(500).json({ message: 'There is an error' });
  }
});

2. Node.js ecosystem: NPM and package.json

The composer.json file in Laravel is equivalent to package.json. This is where you manage all your project’s dependencies (libraries) using a package manager like NPM or Yarn. You’ll often use the npm install command.

3. There is no “Magic” (Everything is Explicit)

Laravel has a lot of “magic” features like Facades (Auth::user()) or global helpers (view()). Express has almost none of that. You have to explicitly import (or require) every module or function you want to use in every file.

4. Error Handling Middleware

Di Laravel, Anda punya App/Exceptions/Handler.php. Di Express, penanganan error adalah sebuah middleware khusus yang biasanya diletakkan di bagian paling akhir dari file aplikasi Anda.

Practical Steps to Get Started

  1. Strengthen Modern JavaScript (ES6+) Fundamentals: Make sure you’re comfortable with const/let, arrow functions (=>), async/await, and import/export.
  2. Set Up a Simple Express Project: Follow the official Express.js tutorial to create a “Hello World” application. Get a feel for how basic routing works.
  3. Choose Your Stack: Make your first decision. For ORM, start with Prisma, as its documentation is excellent and up-to-date.
  4. Create a Simple CRUD API Project: This is the best way to learn. Create an API for a blog (Posts, Users, Comments). This will force you to learn:
    • Routing with Express Router.
    • Connecting Prisma to a database.
    • Creating controller logic for Create, Read, Update, and Delete.
    • Using a validation library like Zod.
  5. Implement Authentication: Once CRUD is running, try adding registration and login using JWT (JSON Web Tokens).