NodeJS: Simple API

I started this post becuase I was trying to integrate some RCON commands with a chat bot, and was going to run an API for pulling down the content of the RCON commands.

I’m still thinking of doing this however I would personally run it in Go because it’s a lot nicer than NodeJS.


NodeJS: Simple API

So I started off wanting to build a super simple API that handles communications between a service I’m creating to integrate my Ark servers chats between one another, and to loop my Discord server in. There are prebuilt solutions but I can say, without looking at many of them, that I hate them. I was going to create my own library to handle inbound POST requests over HTTP because everything I was seeing in search results pushed me towards overengineered solutions however I found the NodeJS Express library and have decided to use that.

The basic idea here to create two services;

  1. First service pulls the Ark servers chat via RCON, and uses RCON to say that message to the other Ark servers. IE if someone in Ark Valguero says ‘hi’ in global, the service will run the say '[user | valguero] hi' command against all other Ark servers
  2. The DiscordJS bot will listen to POST requests on a certain port, and send that message via a Webhook to via a predefined channel.
  3. The DiscordJS bot will also listen to all messages sent to a channel, and submit those messages to the first service (the Ark chat service) via a POST request, which will then relay that message using the say rcon command.

This sounds a little convoluted, but it makes sense in my head.

Why Express?

Their API documentation is simple and concise. It took me longer to write this section of this blog post than it did to understand how to use the library from their docs. DiscordJS could take notes. Then drop DiscordJS as a project. Then sign the rights to Discord over to Danny because honestly, we all know it would be in better hands.

Actually, scratch the above. I had to google it to find this code-for-geek post and use it to create my test app below

A simple Express test app

You need to install stuff:

npm install express body-parser

I created a file called api.js:

const express = require('express')
const app = express();
const router = express.Router();
const bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

router.post('/something', function(req, res) {
    var somekey = req.body.somekey;
    console.log(`- ${somekey} .. somekey sounds like monkey lol`);
    res.end('thanks for the somekey, monkey');
});

app.use('/', router);

app.listen(8080, () => {
    console.log('+ Listening on port 8080');
});

To test the functionality using Powershell, because WSL doesn’t support curl on localhost:8080, because Windows is dogshit, you can use the following script, or code, or whatever Powershell is:

Invoke-WebRequest `
    -UseBasicParsing http://localhost:8080/something `
    -ContentType "application/json" `
    -Method POST `
    -Body '{ "somekey": "this is a value" }'

One feature that I find quite -neat- annoying is Powershell throws an error for an errored-out Invoke-WebRequest, even if the ‘error’ is somehow returned from the endpoint. This is super annoying because you’re not 1000% sure if it’s your command that failed or if it’s something with the endpoints response. Anyway, I digress.

If you run node api.js and then execute the above Powershell, you’ll receive the configured res.end message in your Powershell window and your node console will say ‘this is a value..’ etc.

So we know that it’s working.