Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 1x 1x 1x 1x | /**
* Author: Kieran Plenn
* Date: 10/03/2025
* Description: This is a basic initialization of an API. Instructions were followed from this
* video: https://www.youtube.com/watch?v=-MTSQjw5DrM
*
* Note: Key 'bug' fix was to make sure the app.listen() function was not deleted.
*
* Run: Make sure you're in the Initial_API directory. Enter 'node .' into the terminal to start
* the API. Check http://localhost:8080 for success.
*
* Last Edit (10/10/2025): Changed code to allow for unit testing with Jest
*/
const express = require('express');
const app = express();
const PORT = 8080;
// Middleware to parse JSON
app.use(express.json());
// GET
app.get('/test', (req, res) => {
res.status(200).send({
name: 'Test1',
status: 'test'
});
});
// POST
app.post('/test/:id', (req, res) => {
const { id } = req.params;
const { info } = req.body;
if (!info) {
return res.status(418).send({ message: 'No info!' });
}
res.send({
name: `Test message with info: ${info} and ID: ${id}`,
});
});
// Only start server if this file is run directly
Iif (require.main === module) {
app.listen(PORT, () => console.log(`it's alive on http://localhost:${PORT}`));
}
// Export app for testing
module.exports = app; |