bashCopy code
npm install -g nodemon
bashCopy code
nodemon server1.js
bashCopy code
npm install --save-dev nodemon
jsonCopy code
"scripts": {
"start": "nodemon server1.js"
}
bashCopy code
npm start
<!DOCTYPE html>
<html>
<head>
<title>Book Management System</title>
<script>
// Function to add a new book
async function addBook() {
const title = document.getElementById('title').value;
const author = document.getElementById('author').value;
const id = document.getElementById('id').value;
const response = await fetch('http://localhost:3001/books', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
id,
title,
author
})
});
if (response.status === 201) {
document.getElementById('message').textContent = 'Book added successfully!';
loadBooks();
} else {
document.getElementById('message').textContent = 'Error occurred while adding the book.';
}
}
// Function to display all books
async function loadBooks() {
const response = await fetch('http://localhost:3001/books');
const books = await response.json();
const booksContainer = document.getElementById('books');
booksContainer.innerHTML = '';
books.forEach(book => {
const bookItem = document.createElement('div');
bookItem.textContent = `ID: ${book.id}, Title: ${book.title}, Author: ${book.author}`;
booksContainer.appendChild(bookItem);
});
}
window.onload = function() {
loadBooks();
};
</script>
</head>
<body>
<h1>Book Management System</h1>
<form onsubmit="event.preventDefault(); addBook();">
<label for="id">Book ID:</label><br>
<input type="text" id="id" name="id"><br>
<label for="title">Book Title:</label><br>
<input type="text" id="title" name="title"><br>
<label for="author">Author:</label><br>
<input type="text" id="author" name="author"><br>
<input type="submit" value="Add Book">
</form>
<p id="message"></p>
<h2>All Books</h2>
<div id="books"></div>
</body>
</html>
bashCopy code
mkdir public
const express = require('express');
const path = require('path');
const app = express();
app.use(express.json());
// Serve static files from the "public" directory
app.use(express.static(path.join(__dirname, 'public')));
// rest of your code...
npm install express
const express = require('express');
const app = express();
app.use(express.static('public'));
app.listen(3000, () => {
console.log('Server is listening on port 3000');
});
htmlCopy code
<meta charset="UTF-8">
htmlCopy code
<!DOCTYPE html>
<html>
<head>
<title>Book Management System</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="/styles.css">
<!-- rest of your head content -->
</head>
<!-- rest of your HTML content -->
</html>
htmlCopy code
<meta name="viewport" content="width=device-width, initial-scale=1">
<!DOCTYPE html>
<html>
<head>
<title>Book Management System</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="/styles.css">
<!-- rest of your head content -->
</head>
<!-- rest of your HTML content -->
</html>
cssCopy code
/* styles.css */
body {
background-color: #f9f9f9;
font-family: Arial, sans-serif;
}
h1 {
color: #333;