Sure, I can help you build a simple Node.js application for dream journaling that stores data in JSON format and has a basic web page to interact with it. Here's a step-by-step guide:
Set up the project:
Create a new directory and initialize a Node.js project:
bash Copy code
mkdir dream-journal
cd dream-journal
npm init -y
Install required packages:
bash Copy code
npm install express body-parser fs path
Create the server and routes:
Create a file named app.js and add the following code:
javascript Copy code
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const path = require('path');
const app = express();
const port = 3000;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
app.get('/dreams', (req, res) => {
fs.readFile('dreams.json', 'utf8', (err, data) => {
if (err) {
console.error(err);
res.status(500).send('Error reading dreams file');
} else {
res.json(JSON.parse(data));
}
});
});
app.post('/dreams', (req, res) => {
const dream = req.body;
fs.readFile('dreams.json', 'utf8', (err, data) => {
if (err) {
console.error(err);
res.status(500).send('Error reading dreams file');
} else {
const dreams = JSON.parse(data);
dreams.push(dream);
fs.writeFile('dreams.json', JSON.stringify(dreams), 'utf8', (err) => {
if (err) {
console.error(err);
res.status(500).send('Error writing dreams file');
} else {
res.status(201).send('Dream added successfully');
}
});
}
});
});
app.listen(port, () => {
console.log(`Dream Journal app listening at http://localhost:${port}`);
});
Create the web page:
Create a file named index.html in the same directory and add the following code:
html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dream Journal</title>
</head>
<body>
<h1>Dream Journal</h1>
<form id="dream-form">
<label for="title">Title:</label>
<input type="text" id="title" name="title" required><br><br>
<label for="description">Description:</label><br>
<textarea id="description" name="description" rows="10" cols="30" required></textarea><br><br>
<button type="submit">Add Dream</button>
</form>
<ul id="dream-list"></ul>
<script>
const dreamForm = document.getElementById('dream-form');
const dreamList = document.getElementById('dream-list');
dreamForm.addEventListener('submit', async (event) => {
event.preventDefault();
const formData = new FormData(dreamForm);
const dream = {
title: formData.get('title'),
description: formData.get('description')
};
const response = await fetch('/dreams', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(dream)
});
if (response.ok) {
dreamForm.reset();
loadDreams();
} else {
alert('Error adding dream');
}
});
async function loadDreams() {
const response = await fetch('/dreams');
const dreams = await response.json();
dreamList.innerHTML = '';
dreams.forEach((dream) => {
const li = document.createElement('li');
li.textContent = `${dream.title}: ${dream.description}`;
dreamList.appendChild(li);
});
}
loadDreams();
</script>
</body>
</html>
Run the application:
Start the Node.js server:
Now you can access the Dream Journal application at http://localhost:3000 .
This application allows you to add dreams with a title and description, and it stores the data in a JSON file named dreams.json. The web page displays the list of dreams and allows you to add new ones.