Sure! Here's a simple single-page sticky notes web application that saves data to a text file using Node.js. The front-end and back-end are combined in one HTML file for simplicity.
Single Page Sticky Notes App
1. Create an index.html file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sticky Notes</title>
<style>
body { font-family: Arial, sans-serif; }
#notes-container { margin-bottom: 20px; }
.note { margin: 10px; padding: 10px; border: 1px solid #ccc; border-radius: 5px; }
textarea { width: 100%; height: 50px; }
</style>
</head>
<body>
<h1>Sticky Notes</h1>
<div id="notes-container"></div>
<textarea id="note-input" placeholder="Write a note..."></textarea>
<button id="save-button">Save Note</button>
<script>
const saveButton = document.getElementById('save-button');
const noteInput = document.getElementById('note-input');
const notesContainer = document.getElementById('notes-container');
async function fetchNotes() {
const response = await fetch('/notes');
const notes = await response.json();
notes.forEach(note => addNoteToDom(note));
}
saveButton.addEventListener('click', async () => {
const note = noteInput.value;
if (note) {
const response = await fetch('/notes', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ note })
});
const data = await response.json();
addNoteToDom(data.note);
noteInput.value = '';
}
});
function addNoteToDom(note) {
const noteDiv = document.createElement('div');
noteDiv.className = 'note';
noteDiv.textContent = note;
notesContainer.appendChild(noteDiv);
}
fetchNotes();
</script>
<script src="https://cdn.jsdelivr.net/npm/express@4.17.3/index.js"></script>
<script>
const express = require('express');
const fs = require('fs');
const app = express();
const PORT = 3000;
app.use(express.json());
app.use(express.static('.')); // Serve current directory files
app.get('/notes', (req, res) => {
fs.readFile('notes.txt', 'utf8', (err, data) => {
if (err) return res.sendStatus(500);
const notes = data ? data.split('\n').filter(note => note) : [];
res.json(notes);
});
});
app.post('/notes', (req, res) => {
const newNote = req.body.note;
fs.appendFile('notes.txt', newNote + '\n', (err) => {
if (err) return res.sendStatus(500);
res.json({ note: newNote });
});
});
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});
</script>
</body>
</html>
Instructions to Run the App:
-
Save the code above in a file named index.html.
-
Create a new file named notes.txt in the same directory. It can be empty initially.
-
Install Node.js if you haven't already.
-
Open your terminal and navigate to the folder containing index.html and notes.txt.
-
Run the following commands to set up the server:
npm init -y # Creates package.json
npm install express fs
-
Serve the application by running:
node index.html
-
Open your browser and go to http://localhost:3000. You should see your sticky notes app!
Note:
This solution combines both front-end and back-end code within the same HTML file, making it constrained but suitable for simple testing. In a production scenario, it's advisable to separate your front-end and back-end code for better organization and scalability.