Build a simple in-memory key-value store and expose it through an Express HTTP API.
The goal of this challenge is to practice:
- Creating and using JavaScript classes
- Designing REST-style HTTP endpoints
- Working with Express route parameters
- Handling JSON request bodies
- Returning appropriate HTTP status codes
- Connecting a simple frontend interface to an API
Requirements
1. KeyValueStore Class
Create a KeyValueStore class inside store.js.
Each instance should maintain its own in-memory collection of key-value pairs.
Implement the following methods:
set(key, value)
Stores a value under the provided key.
If the key already exists, its value should be replaced.
store.set("name", "Alice");
get(key)
Returns the value associated with the key.
If the key does not exist, return undefined.
store.get("name");
delete(key)
Deletes the key from the store.
Return:
true if the key existed and was deleted
false if the key did not exist
has(key)
Returns true if the key exists and false otherwise.
getAll()
Returns an object containing every key-value pair currently stored.
Example:
{
"name": "Alice",
"language": "JavaScript"
}
HTTP API
Create an Express application in app.js.
The server must support the following endpoints.
Set a value
PUT /store/:key
Request body:
{
"value": "Alice"
}
Successful response:
Status: 200
{
"key": "name",
"value": "Alice"
}
The value property is required.
If it is missing, return:
Status: 400
{
"error": "Value is required"
}
Get a value
GET /store/:key
If the key exists:
Status: 200
{
"key": "name",
"value": "Alice"
}
If the key does not exist:
Status: 404
{
"error": "Key not found"
}
Delete a value
DELETE /store/:key
If the key exists and is deleted:
Status: 200
{
"message": "Key deleted"
}
If the key does not exist:
Status: 404
{
"error": "Key not found"
}
Get all values
GET /store
Return all currently stored key-value pairs.
Status: 200
Example:
{
"name": "Alice",
"language": "JavaScript"
}
Web Interface
A frontend interface is provided in index.html.
Your Express application must serve this file when a user visits:
GET /
The page interacts with the API that you build.
Important Notes
- Store all data in memory.
- Do not use a database.
- Your key-value store must be implemented using a class.
- Export the
KeyValueStore class from store.js.
- Export the Express application from
app.js.
- Do not call
app.listen() inside app.js.
index.js already starts the server for you.
- Data does not need to survive a server restart.