Techindika · Technical interview

Read this file and tell us what you would change

This is a route file from one of our live client applications. Names have been changed. Nothing else has been edited, including the parts we are not proud of.

Take a minute to read it, then talk us through what you would change and in what order you would change it.

Assume it is running right now on the same server as the rest of our production estate: roughly twenty-five products and sixty-odd Node processes, on one box, with one disk.

There is no trick here and no single right answer. Say what you see, say what you are unsure about, and tell us which one you would fix first. Thinking out loud is the point — we are more interested in how you read code than in whether you spot everything.

routes/orders.js Node.js · Express · Mongoose · 31 lines
1// routes/orders.js - taken from one of our live client applications,
2// names changed, otherwise unedited.
3
4const express = require('express')
5const router = express.Router()
6const Order = require('../models/Order')
7
8router.get('/orders', async (req, res) => {
9 const orders = await Order.find({})
10 const out = []
11 for (const o of orders) {
12 const user = await User.findById(o.userId)
13 out.push({ ...o.toObject(), userName: user.name })
14 }
15 res.json(out)
16})
17
18router.post('/orders', (req, res) => {
19 const order = new Order(req.body)
20 order.save()
21 res.json({ ok: true, id: order._id })
22})
23
24router.get('/orders/:id/invoice', async (req, res) => {
25 const o = await Order.findById(req.params.id)
26 const pdf = await buildInvoicePdf(o)
27 fs.writeFileSync('/tmp/' + req.params.id + '.pdf', pdf)
28 res.download('/tmp/' + req.params.id + '.pdf')
29})
30
31module.exports = router

Click a line number to mark that line, so you and the interviewer are looking at the same place. Click it again to clear it.