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;
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 serverssay
rcon command.This sounds a little convoluted, but it makes sense in my head.
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
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.