Create Your Next Microservice with Merver!

What is Merver?

In my effort to better understand the NodeJS core libraries I started playing with the HTTP library trying to recreate many of the features I enjoy from frameworks like express, fastify and polka. In doing so I ended up creating a fairly useable micro-web framework that I call Mever.

Getting Started

You can install the library and follow the documentation to get you started but I have a template you can scaffold that has all the initial setup done for you, just run the following command.

npx merced-spinup merver projectName

Change directory into the new folder and run npm install

You have to two scripts

npm run dev to run in development mode npm start to run in production mode

Configuration

In the configuration folder you have two files…

In this file, you can configure your server such as cors headers, static folders and more.

configure your database connection in this file

middleware/globals.js

Here is where you can define global middleware, (route middleware is configured in your route objects). Pass any middleware into the addMiddlware function which you’ll see in the existing examples.

What is going on is each middleware is being registered to a middler object which when invoked will run all the middleware registered to it passing the request and response object to each one.

The server runs a middler object before running a responder object with all the routes when handling a request.

By default, a basic logger and bodyParser(json and urlencoded) middleware is already configured.

controllers/responses/

In this folder create and export your response objects (routes). Each endpoint gets one object where you can make a handler for the particular verb like so…

module.exports = {
endpoint: '/',
GET: (req, res) => {res.html(`<h1>It Works!</h1>`)},
POST: (req, res) => {res.json({it: 'works'})},
PUT: (req, res) => {res.json({it: 'works'})},
DELETE: (req, res) => {res.json({it: 'works'})}
}

then import the object into controllers/responder.js

const {Responder} = require('am-merver')
const responder = new Responder()
// Import Responses
const root = require('./responses/root')
// Register Responses
responder.newResponse(root)
// Export Responder
module.exports = responder

That’s it

Write your routes like you normally would. Url Params and Url queries work just like you’d expect them to work in express.

--

--

--

Alex Merced is a Full Stack Developer, learn more about his work at AlexMercedCoder.com

Love podcasts or audiobooks? Learn on the go with our new app.

From nothing to 5 decent leads a week as a freelancer

Vue.js | How to Structure a Vue.js Project

Creating accessible progress indicator in React

Everything you need to know about JavaScript Strings.

Integrating ReactViro with existing React Native projects

Domain-ization in Angular

React with fire base push notification

Basic Period selector using HTML and JavaScript

Period Selector

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alex Merced Coder

Alex Merced Coder

Alex Merced is a Full Stack Developer, learn more about his work at AlexMercedCoder.com

More from Medium

Near Realtime Data Monitoring: A Simple Javascript

MongoDB as a Secondary Database with Strapi

Electron: Getting Custom Error Messages from IPC Main

Beginning with KoaJS and Creating Server using Koa