//console.log("Hello, World!"); const express = require('express'); const mongoose = require('mongoose'); const app = express(); const PORT = 3000; // Middleware app.use(express.json()); // Connect to MongoDB mongoose.connect('mongodb://localhost:27017/bookDB', { useNewUrlParser: true, useUnifiedTopology: true, }); // Define the Book schema const bookSchema = new mongoose.Schema({ title: String, author: String, summary: String, }); const Book = mongoose.model('Book', bookSchema); // Routes // Add your routes for CRUD operations here // Create a new book app.post('/books', async (req, res) => { const { title, author, summary } = req.body; try { const book = await Book.create({ title, author, summary }); res.status(201).json(book); } catch (error) { res.status(500).json({ error: 'Could not create book' }); } }); // Get all books app.get('/books', async (req, res) => { try { const books = await Book.find(); res.json(books); } catch (error) { res.status(500).json({ error: 'Could not retrieve books' }); } }); // Get a specific book by ID app.get('/books/:id', async (req, res) => { const { id } = req.params; try { const book = await Book.findById(id); res.json(book); } catch (error) { res.status(404).json({ error: 'Book not found' }); } }); // Update a book by ID app.put('/books/:id', async (req, res) => { const { id } = req.params; const { title, author, summary } = req.body; try { const book = await Book.findByIdAndUpdate(id, { title, author, summary }, { new: true }); res.json(book); } catch (error) { res.status(404).json({ error: 'Book not found' }); } }); // Delete a book by ID app.delete('/books/:id', async (req, res) => { const { id } = req.params; try { await Book.findByIdAndRemove(id); res.json({ message: 'Book deleted successfully' }); } catch (error) { res.status(404).json({ error: 'Book not found' }); } }); // Start the server app.listen(PORT, () => { console.log(`Server is running on port ${PORT}`); });
Write, Run & Share NodeJS code online using OneCompiler's NodeJS online compiler for free. It's one of the robust, feature-rich online compilers for NodeJS language,running on the latest LTS version NodeJS 16.14.2. Getting started with the OneCompiler's NodeJS editor is easy and fast. The editor shows sample boilerplate code when you choose language as NodeJS and start coding. You can provide the dependencies in package.json
.
Node.js is a free and open-source server environment. Node.js is very popular in recent times and a large number of companies like Microsoft, Paypal, Uber, Yahoo, General Electric and many others are using Node.js.
Google chrome's javascript engine V8
and is pretty fast.Asynchronous
, event-driven
and works on single-thread model
thus eliminating the dis-advantages of multi-thread model.Express is one of the most popular web application framework in the NodeJS echosystem.
let moment = require('moment');
console.log(moment().format('MMMM Do YYYY, h:mm:ss a'));
const _ = require("lodash");
let colors = ['blue', 'green', 'yellow', 'red'];
let firstElement = _.first(colors);
let lastElement = _.last(colors);
console.log(`First element: ${firstElement}`);
console.log(`Last element: ${lastElement}`);
Following are the libraries supported by OneCompiler's NodeJS compiler.