Express.js, Django & Laravel - The Backend Badasses
In this article, I’m going to explain Django REST Framework and, at the same time, compare it to ExpressJS (NodeJS Framework) and Laravel (PHP Framework).
Basics
Express.js
If you check out the home page of ExpressJS, you will find it describing itself as a minimal and flexible Node.js web application framework. It is because Express.js is nothing but a very thin layer over Node.js, which is a Javascript runtime environment. Node.js, in its raw form, is very difficult to implement and Express.js hides all the complexities that lie within. We are able to use Javascript on the server-side because of Node.js and easily because of Express.js. Among all the framework's that Node.js has, Express.js has the most minimalistic implementation.
Laravel
PHP is most powerful full-stack web framework out there. It has an easy implementation, great community support (which only comes after a framework has proved it's worth), has a great guide to their implementation and its architecture makes it stand out. Laravel is already preloaded with things like sessions, cookies, caching management and are easily configured using their config files. With the help of its command-line tool artisan, Laravel makes development fast and keeps us unbothered from the trouble of maintaining a proper structure.
Django REST
A framework that claims that you can develop your applications much faster using it and is truly acknowledged for that is Django. Django is Python's most popular web framework and a great option for your higher scalable applications ready to be deployed after running some checks on its setting.py file, which assures you of the security of your application. Fast development and great security are the two things Django provides.
Let's talk about Django REST. Well, Django REST is a more abstract version of Django solely abiding by the Representational State Transfer principle. Django REST has subclassed and modified the Django classes to provide the user with more layered and we will handle most of the things so you don't have to worry approach.
Structure
Express.js
Laravel
We can generate a Laravel app using Laravel installer or composer. Both will give you the structure as shown below:
Django REST
Server (HTTP/HTTPS)
ExpressJs
const app = require('express')();
app.listen(8000, ()=> {
console.log('Listening');
});
const https = require('https');
const app = require('express')();
const fs = require('fs');
const options = {
key: fs.readFileSync('key.pem'), // SSL certificate key
cert: fs.readFileSync('cert.pem') // SSL certificate
};
https.createServer(options, app).listen(8000);
Laravel
Django REST
// Django implementation to create application callable
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings')
application = get_wsgi_application()
// A WSGI application
class Application(object):
def __call__(self, environ, start_fn):
start_fn('200 OK', [('Content-Type', 'text/plain')])
yield "Hello World!\n"
app = Application()
// Redirect your application to HTTPS
SECURE_SSL_REDIRECT = os.getenv('SECURE_SSL_REDIRECT') == 'True'
// Allow cookies set from only HTTPS connection
SESSION_COOKIE_SECURE = os.getenv('SESSION_COOKIE_SECURE') == 'True'
// Allow CSRF cookies from only HTTPS connection
CSRF_COOKIE_SECURE = os.getenv('CSRF_COOKIE_SECURE') == 'True'
Request Validation
Express.js
Laravel
Django REST
Express.js
let myLogger = function (req, res, next) {
console.log('LOGGED')
next()
}
app.use(myLogger)
let session = require('express-session')
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: true,
cookie: { secure: true }
}))
let app = express()
app.use(cookieParser())
//Use secret to sign cookies
app.use(cookieParser('iwontgivemycookie', {}));
const rateLimit = require("express-rate-limit");
const apiLimiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100
});
// Apply it before hitting any endpoint
app.use("/api/", apiLimiter);
Laravel
class CheckAge
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if ($request->age <= 200) {
return redirect('home');
}
return $next($request);
}
}
Django REST
class SimpleMiddleware:
def __init__(self, get_response):
self.get_response = get_response
# One-time configuration and initialization.
def __call__(self, request):
# Code to be executed for each request before
# the view (and later middleware) are called.
response = self.get_response(request)
# Code to be executed for each request/response after
# the view is called.
return response
ORM
Express.js
Laravel
Django REST
CRON & Queues
Express.js
Laravel
Django REST
Conclusion
Phew! You made it this far.
If you are new to the programming world & after going through this documentation, you might be thinking very low of Express.js. Well, you should not. Express.js was not created as a full-stack web framework. It was created to make the dream that Node.js gave us - Running Javascript at server-side a lot more developer-friendly and easy to use. Though Express.js make us wander around the web, go through the documentation of modules and integrate it into your application on your own, but it gives you a much more detailed overview of your functionality. Express.js doesn't hold back in performance and scalability due to this. You can always choose Express.js if you want a fast performing, highly scalable server and want to explore the web a bit more.
Laravel already wins in lots of scenarios in this article. It is loaded with the necessities that an application needs for a good head start and is easily configurable. Laravel's great documentation makes it easy for any developer to work with it.
Go with Django if you want your application to develop at a faster speed and are concerned about the security of your application. Sites like Pinterest, The Washington Post, Instagram are built using Django, so you can't question the scalability of Django. Its DRY principle is the reason Django is able to do so much with very few lines of code and doesn't get heavy as the application continues to grow.
Performance of each of this framework is excellent, so don't worry about that.
At the end You cannot judge a framework in an absolute sense. It all depends on your use case.
Thank you for reading!