Serving static content, REST endpoints and Websockets with Express and node.js

I’ve yet to see a framework that is as simple as Express for developing REST endpoints. I’m experimenting with a React app that receives push updates from the server using Websockets. Is it possible to use Express to serve all the requests for this app: static content (the React app), REST endpoints and Websocket? Turns out, yes, and it’s pretty easy too.

Starting first using Express to serve static content:

This uses the static middleware for serving the static content.

Handling REST requests with Express is simple using the get(), post(), put(), and delete() functions on the Router. Adding an example for a GET for /status now we have this:

 

Next, adding support for Websockets, using the ws library. Incrementally adding to the code above, now we create a WebSocket.Server, using the option to pass in the already created HTTP server:

const wss = new SocketServer({ server });

At this point we add callbacks for ‘connection’ and ‘message’ events, and we’re in business:

This is the starting point for a React app to build a websockets client, more on that in a future post. The code so far is available in this github repo: https://github.com/kevinhooke/NodeExpressStaticRESTAndWebsockets

Node.js online courses and self-paced tutorials

I’m always looking for resources for learning something new. I’m currently digging into some Node.js. I’ve been working through a Udemy course, ‘The Complete Node.js Developer Course’, and while the quality of the materials and videos is very good, I’m finding the initial pace of the videos a bit slow for my liking (you can run them at 1.5x and 2x which helps to get through them quicker). If you were starting from zero background in JavaScript then the initial pace is probably spot on, but if you have some background already, then you might want to skip ahead.

Looking for other sources, I came across http://nodeschool.io/ . The interesting thing with their approach is they have Node.js apps that drive your tutorials and assess your solutions to the puzzles. I’ve been working though learnyounode and so far it’s going pretty well. Install with:

npm install -g learnyounode

Refreshing a JAX-RS backend with some Node.js and Express?

Over the past few year or so, I’ve been building a web app that visualizes amateur radio spots using a digital mode called JT65. The site is currently up and live here: http://www.spotviz.info/#/home

I started building this as an exercise to learn some AngularJS 1.x (I posted a number of posts along the way too). The backend datastore is MongoDB, and there’s a JAX-RS War deployed to WildFly that provides a REST backend to the AngularJS frontend. The majority of the logic for the webapp is all in the AngularJS app.

Since playing with some Node.JS and Express a couple of weeks ago, I’ve been considering if I should have a go at replacing the JAX-RS code with a Node.js backend too. Since the existing code is mainly building and executing MongoDB queries, this wouldn’t be that hard to do a straight replace. Based on what I’ve seen so far of libraries like Mongoose, the replacement code is likely to be significantly more concise than the existing Java based backend. I’ll queue this up for a project in the coming weeks 🙂