[Koa]404 while passing throught the routeur - node.js

I'm having some trouble with the Koa framework. I'm trying to build a pretty basic server by I'm having a problem with my router. The ctx always return 404 despite passing in my functions.
Some code :
//www.js
const Koa = require('koa');
const app = new Koa();
const version = require('./routes/version');
app.listen(config.port, () => {
console.log('Server is listenning on port ' + config.port);
});
app.use(version.routes());
app.use(ctx => {
console.log ('test')
});
//version.js
const Router = require('koa-router');
const router = new Router();
router.prefix('/version');
router.use((ctx, next) => {
ctx.vFactory = new VersionFactory(ctx.app.db);
next();
});
router.get('/', getAllVersions);
async function getAllVersions(ctx, next) {
const ret = await ctx.vFactory.getAllVersions();
ctx.body = JSON.stringify(ret.recordset);
console.log(ctx.body)
await next();
}
I've checked a few threads. Most of the time, the problem seems to come from a non Promise based function in the await part of the router function. Here it is a simple DAO using mssql which is pretty promise based.
class DaoVersion {
constructor(db) {
this.pool = db;
}
async getAllVersions() {
const me = this;
return new Promise((resolve) => {
const ret= me.pool
.query(getVersion);
resolve(ret);
});
}
}
The console output seems good. I have my ctx.body set with my db data but if I try to check the whole context, I still have a 404. More interesting, if I try to ctx.res.write (using default node response) I got the "already end" message. So it seems Koa have sent the message before passing threw my function.
Any idea why and how I could correct that ?

Koa default response.status code is 404, unlike node's res.statusCode which defaults to 200.
Koa changes the default status code to 200 - when your route set's a non empty value to ctx.body or in some cases you can manually change (like if you need to set it to 202) it by using ctx.status = xxx.
You can use this documentation for reference: https://github.com/koajs/koa/blob/master/docs/api/response.md
Also, your route should be an async function:
router.get('/', async(ctx, next) => {
ctx.body = await getAllVersions
await next()
}

Related

REST Post API - retrieve result from TinyURL method

I failed to get a result from TinyURL within the POST method and assign it to "short_url" for the response. The console.log(short_url) will show "Promise { pending }". I tried async / await function to retrieve the TinyURL result but I'm sure I'm not using it right.
var express = require('express')
var TinyURL = require('tinyurl')
var app = express()
app.use(express.json())
app.use(express.static('public'))
app.get("/", function (req, res) {
res.sendFile(__dirname + '/index.html')
})
app.post('/api/shorturl', (req, res) => {
let original_url = req.body.url
console.log(original_url) // this one shows correct URL from request body
async function createShortURL(url) {
await TinyURL.shorten(url, function(res) {
console.log(res) // this one shows correct shortened URL
}
)}
let short_url = createShortURL(original_url)
console.log(short_url) // this one shows "Promise { <pending> }"
res.json({
original_url : original_url,
short_url : short_url
})
})
var listener = app.listen(process.env.PORT || 3000, function () {
console.log('Your app is listening on port ' + listener.address().port)
})
You're mixing async/await and callback. Don't do that. The tinyurl library provides the Promise version of shorten method. We can use async/await directly.
app.post('/api/shorturl', async (req, res) => {
let original_url = req.body.url
console.log(original_url) // this one shows correct URL from request body
// just this
let short_url = await TinyURL.shorten(url);
console.log(short_url)
res.json({
original_url : original_url,
short_url : short_url
})
})
EDIT
If you're using callback, please be aware of callback hell. It's one of the main reasons why people prefer async/await.

route is sending empty response

I have this controller
const getBalance = async (address, network) => {
const web3 = getWeb3Instance(network);
const currentBalance = await web3.eth.getBalance(address);
const formattedBalance = web3.utils.fromWei(currentBalance, 'ether');
return formattedBalance
};
This is how I use it in the route:
router.post('/getBalance', (req, res) => {
const {address,network} = req.body
try {
res.status(200).send(controllers.getBalance(address,network))
} catch (e) {
res.status(404).send(`${e}`)
}
});
When I console.log(formattedBalance) it logs the correct answer but in the response it is sending empty object {} and I don't know why. I'm using node.js with express and web3.
Any suggestions please?
You have the word async infront of your function. An function has an different behaviour if you put async infront of it. It acts like an promise.
Did you tried console.log(controllers.getBalance(address,network))? In the browser you would see Promise { <pending> }
The problem is you sending an pending promise back.
Change it to this. You need to wait till the promise resolves
router.post('/getBalance', async (req, res) => {
const {address,network} = req.body
try {
let balance = await controllers.getBalance(address,network);
res.status(200).send(balance)
} catch (e) {
res.status(404).send(`${e}`)
}
});

Chaining async await calls in Node/Express with an external time limit

I'm building a Slackbot that makes a call to an Express app, which then needs to 1) fetch some other data from the Slack API, and 2) insert resulting data in my database. I think I have the flow right finally using async await, but the operation is timing out because the original call from the Slackbot needs to receive a response within some fixed time I can't control. It would be fine for my purposes to ping the bot with a response immediately, and then execute the rest of the logic asynchronously. But I'm wondering the best way to set this up.
My Express route looks like:
const express = require('express');
const router = express.Router();
const knex = require('../../db/knex.js');
const slack = require('../../services/slack_helpers');
// POST api/slack/foo
router.post('/foo', async (req, res) => {
let [body, images] = await slack.grab_context(req);
knex('texts')
.insert({ body: body,
image_ids: images })
.then(text => { res.send('worked!'); }) // This sends a response back to the original Slackbot call
.catch(err => { res.send(err); })
});
module.exports = router;
And then the slack_helpers module looks like:
const { WebClient } = require('#slack/web-api');
const Slack = new WebClient(process.env.SLACKBOT_TOKEN);
async function grab_context(req) {
try {
const context = await Slack.conversations.history({ // This is the part that takes too long
channel: req.body.channel_id,
latest: req.headers['X-Slack-Request-Timestamp'],
inclusive: true,
limit: 5
});
} catch (error) {
return [error.toString(), 'error'];
}
return await parse_context(context);
};
function parse_context(context) {
var body = [];
context.messages.forEach(message => {
body.push(message.text);
});
body = body.join(' \n');
return [body, ''];
}
module.exports = {
grab_context
};
I'm still getting my head around asynchronous programming, so I may be missing something obvious. I think basically something like res.send perhaps needs to come before the grab_context call? But again, not sure the best flow here.
Update
I've also tried this pattern in the API route, but still getting a timeout:
slack.grab_context(req).then((body, images) => {
knex ...
})
Your timeout may not be coming from where you think. From what I see, it is coming from grab_context. Consider the following simplified version of grab_context
async function grab_context_simple() {
try {
const context = { hello: 'world' }
} catch (error) {
return [error.toString(), 'error']
}
return context
}
grab_context_simple() /* => Promise {
<rejected> ReferenceError: context is not defined
...
} */
You are trying to return context outside of the try block where it was defined, so grab_context will reject with a ReferenceError. It's very likely that this error is being swallowed at the moment, so it would seem like it is timing out.
The fix is to move a single line in grab_context
async function grab_context(req) {
try {
const context = await Slack.conversations.history({
channel: req.body.channel_id,
latest: req.headers['X-Slack-Request-Timestamp'],
inclusive: true,
limit: 5
});
return await parse_context(context); // <- moved this
} catch (error) {
return [error.toString(), 'error'];
}
};
I'm wondering the best way to set this up.
You could add a higher level try/catch block to handle errors that arise from the /foo route. You could also improve readability by staying consistent between async/await and promise chains. Below is how you could use async/await with knex, as well as the aforementioned try/catch block
const express = require('express');
const router = express.Router();
const knex = require('../../db/knex.js');
const slack = require('../../services/slack_helpers');
const insertInto = table => payload => knex(table).insert(payload)
const onFooRequest = async (req, res) => {
try {
let [body, images] = await slack.grab_context(req);
const text = await insertInto('texts')({
body: body,
image_ids: images,
});
res.send('worked!');
} catch (err) {
res.send(err);
}
}
router.post('/foo', onFooRequest);
module.exports = router;

Is it bad practice to perform http requests in main module?

I want to sync data from an external resource (file, server etc) into my DB every time the server starts.
app.js
const app = express();
app.use('/add', require('./addLayer'));
...
(async() => {
const external_resource = await axios.get('...'); // get data
await axios.post('http://localhost:3000/add', external_resource.data); // add it to db
});
addLayer.js
const DBObject = require('../../models/DBObject')
const addNewLayer = async (req, res) => {
try {
const data = validateData(req.body)
await DBObject.create(data)
} catch (err) {
res.status(400).send(err)
}
}
I do not want to re-write the code in the /add route (which includes data validations), but I find performing POST requests in here not good for some reason. Is there a better way to do so?

Jest Express testing middleware with arguments

I'm pretty new to node and this is my first time unit testing an app. I'm doing well with Jest faking the request with Jest function as below
// Create a fake request
const mockRequest = (sessionData, body) => ({
session: { data: sessionData },
body
});
// Create a fake response
const mockResponse = () => {
const res = {};
res.status = jest.fn().mockReturnValue(res);
res.json = jest.fn().mockReturnValue(res);
return res;
};
const mockNext = () => {
const next = jest.fn();
return next;
};
So I can use them like follows
doSomething(req, res, next);
expect(res.status).toHaveBeenCalledWith(201);
//or
expect(next).toHaveBeenCalled();
That's enough for all the cases until I found that my authorisation middleware includes a couple of parameters so I can not pass the fake res and req as below
exports.isAllowedTo = (par1, par2) => {
return async (req, res, next) => {
try {
//
// Grant logic here that needs par1 and par2
//
if(granted)
next();
else
return res.status(401).json({
error: "You don't have enough permission to perform this action"
});
} catch (err) {
res.status(406).json({
error: err.toString(),
})
}
}
}
If I test isAllowTo(req, res, next) with the mock req, res and next then I'm missing the 2 parameters needed by the function. Actually when I do this, the function isAllowTo() is not even called. I don't know how to deal with that. Any suggestion or approach?
Two months later I realized that the real problem is that I'm testing a function inside of another function.
So firstly I store the function in a variable so I can test it as a regular middleware.
test('Grant access if user role is allowed to', async () => {
const isAllowToTester = userController.isAllowedTo(par1, par2);
await isAllowToTester(req, res, next)
expect(next).toHaveBeenCalled();
});
Hope this helps someone else.
Credits to this post
Check out https://github.com/nock/nock it's a library dedicated to mocking requests and responses, it's really easy to use with unit tests/jest. I personally don't think is worth it to write your own mocking implementation.

Resources