Building a simple API in Node.js using Vaxic

Building awesome apps takes awesome backend skills. I’ll be showing you how to build a simple API using Node.js and the Vaxic library.

To get started, you’ll need to install Vaxic to your package.

npm install vaxic

Now let’s get right to work.

We’ll start by creating a new file and requiring the Vaxic library. Let’s call the file index.js.

const V = require('vaxic')

Now we’re ready to create our app (Vaxicinstance).

const app = new V()

And let’s add a simple endpoint. This one will be a GET endpoint for the path /api/names`.

app.add('GET', '/api/names', (req, res) => {
res.writeHead(200, {'Content-Type': 'application/json'})
res.end(JSON.stringify(['Ethan', 'James', 'Jason', 'Justin']))

Aaand, there we go. A simple GET endpoint (handle) was added to our API!

Now let’s do something else. Let’s add a POST endpoint.

app.add('POST', '/api/log', (req, res) => {
console.log('Received message: ' + req.body.toString())
res.writeHead(200, {'Content-Type': 'application/json'})
'success': 'Logged message.'

Now we need to actually let our API listen.

app.listen(8080, 'localhost', () => {
console.log('Listening. ;)')

Simple enough, right? Let’s see where you go from there.

G’luck with your API!