Choosing Between Spark and Express

Both Spark and Express frameworks are really similar functionally and syntactically. Either is really great to create robust and fast APIs or building websites faster.

You can make a better choice if you know enough about each technology.

At the end of this post I reveal my personal preference.

Java Spark and the Express framework are a good choice for creating RESTful APIs in a fast and clean manner.

Spark is a microframework and Express is a minimalist framework that extends its capabilities through modules.

Similarities between Frameworks

Routing
Both frameworks store the routes and the HTTP verb internally. This enables your app to have a faster start.

Spark makes use of lambda expressions so both frameworks have fairly similar syntax.

Spark
get("/home", (req,res) -> {
   Model model = new Model();
   return new ModelAndView(model, "index.hbs");
}, new HandlebarsTemplateEngine());
Express
router.get('/home', (req, res, next) =>  {
 const model = new Model();
 res.render('index', { model });
});

Middleware

A middleware is a block of code that is executed during the request-response cycle. Both Spark and Express make the process of creating a middleware very easy. Spark calls this middleware filters.

Spark
before("/home", (req, res) -> {
   System.out.println("middle ware working");
});
Express
app.use('/home', (req, res, next) => {
   console.log("middle ware working");
   next(); });

Error Handling

Both frameworks have a fancy way to handle errors. In the following example we can see how Spark throws an exception, which triggers the method called exception.

In Express we pass an error as a parameter of the method called next. This triggers the middleware that has the err parameter.

Spark
get("/throwexception", (request, response) -> {
   throw new YourCustomException();
});
exception(YourCustomException.class, (exception, request, response) -> {
   response.status(404);
   response.body("recurso no encontrado");
});
Express
app.use((req, res, next) =>  {
 var error = new Error("recurso no encontrado ");
 next(error);
});

app.use((err, req, res, next) => {
 res.status(404);
 res.render(err.message);
});

Differences between Frameworks

Server Initialization

When it comes to servers, Spark uses Jetty, an Open Source server and Express uses the native module http of Node for server initialization.

Spark
public Server create(int maxThreads, int minThreads, int threadTimeoutMillis) {
   Server server;
   if (maxThreads > 0) {
       int max = maxThreads;
       int min = (minThreads > 0) ? minThreads : 8;
       int idleTimeout = (threadTimeoutMillis > 0) ? threadTimeoutMillis : 60000;
       server = new Server(new QueuedThreadPool(max, min, idleTimeout));
   } else {
       server = new Server();
   }
   return server;
}
Express
var app = require('../app');
var http = require('http');

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

var server = http.createServer(app);
server.listen(port);

It is worth highlighting that for Spark to initialize the server there should be at least one route or middleware. This is not the case with Express, where it is not necessary to have routes declared before initializing the server.

Websockets

Spark offers a protocol to enable a bidirectional communication between the server and the client. Express resorts to install libraries like Socket.io.

If you want to make use of websockets on a Spark project, you will need to use the default server that come with it and you’ll also need to define it before regular http routes.

Sessions

Spark creates a session that can be useful to store data by default. In Express you can download a module to have this functionality.

Conclusion

It’s hard to say if one framework is better than the other. They both have pros and cons.

Spark has many handy built-in features, while Express needs to download them as separate modules.

Express offers you a lightweight framework that can be very flexible. Spark has the benefits of robustness and security thanks to the use of Java.

I personally prefer the use of Express, as I have more experience with it. Yet I’m open-minded and might use Spark in the near future.

If you have any questions or comments, please contact me at [email protected].

Focus Mode

Contact Request

Close

We will call you right away. All information is kept private