Eric's Advanced Web Scripting Workspace

Butler Community College Spring 2026

Introduction

We made this simple script to show off what we can do with the API we made.

You can go to data.js to view the API.

const express = require('express');
const app = express();
const {items} = require('./data');

These are all imports. The first line imports the express library. The second line calls express as a function stores it in 'app'. The third line imports our API.

app.get('/', (req, res) => {
    res.send('<h1>Rock, Paper, Scissors Ring</h1> <a href="/api/items">Available Items</a>');
});

This line just sends some simple HTML to the homepage.

app.get('/api/items', (req, res) => {
    const newListing = items.map((item)=>{
        const {name, desc, strength, weakness} = item;
        return {name, desc, strength, weakness}
    });
    res.json(newListing);
});

When the user vists the url '/api/items', the server starts to loop through every item in the 'items' array and returns a brand new array called newListing. It pulls out the four fields, 'name, desc, strength, weakness' and builds a new object before sending it as a JSON response.

app.get('/api/items/:itemID', (req, res)=> {
    console.log(req.params);
    const {itemID} = req.params;
    const singleItem = items.find((item)=>item.id === Number(itemID));
    res.json(singleItem);
});

This lets you add any number to the end of the URL like, '/api/items/5'. This searches the 'items' array for the first item whose id matches 'itemID'. Finally, it sends the matched item back as a JSON response.

app.get('/query', (req, res) => {
    const {search, limit} = req.query;
    let sorted = [...items];
    if(search) {
        sorted = sorted.filter((item)=>{
            return item.desc.match(search);
        });
    }
    if (limit) {
        sorted = sorted.slice(0, Number(limit));
    }
    if (sorted.length < 1) {
        return res.status(200).json('No items matching your query were found.');
    }

    res.status(200).json(sorted);
});

I think this sorts through the sorted array and filters it based off the search value (if one was provided). It can also trim down the array if a limit value was provided. Lastly, it has a fallback in case no items survived the filtering.

app.listen(8080, () => {
    console.log('Server is running...');
});

This line makes the server listen on port 8080 and sends a message in the console for debugging purposes.