Develop a Node.js application that connects to a MongoDB database, stores information about various movies (title, director, release date, etc.), and provides CRUD operations through RESTful API endpoints.
PLAINFORMAT
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const Movie = require('./movie');

// Replace this URL with your own MongoDB connection string
const mongoDBURL = 'mongodb://localhost:27017/moviesdb';

mongoose.connect(mongoDBURL, { useNewUrlParser: true, useUnifiedTopology: true })
  .then(() => console.log('Connected to MongoDB'))
  .catch(err => console.error('Could not connect to MongoDB', err));

const app = express();
app.use(bodyParser.json());

const port = process.env.PORT || 3000;
app.listen(port, () => {
  console.log(`Listening on port ${port}...`);
});

// GET all movies
app.get('/api/movies', async (req, res) => {
  const movies = await Movie.find();
  res.send(movies);
});

// GET a single movie by ID
app.get('/api/movies/:id', async (req, res) => {
  const movie = await Movie.findById(req.params.id);
  if (!movie) return res.status(404).send('Movie not found');
  res.send(movie);
});

// POST a new movie
app.post('/api/movies', async (req, res) => {
  const movie = new Movie({
    title: req.body.title,
    director: req.body.director,
    releaseDate: req.body.releaseDate,
  });
  await movie.save();
  res.send(movie);
});

// PUT (update) an existing movie
app.put('/api/movies/:id', async (req, res) => {
  const movie = await Movie.findByIdAndUpdate(req.params.id, {
    title: req.body.title,
    director: req.body.director,
    releaseDate: req.body.releaseDate,
  }, { new: true });

  if (!movie) return res.status(404).send('Movie not found');
  res.send(movie);
});

// DELETE a movie
app.delete('/api/movies/:id', async (req, res) => {
  const movie = await Movie.findByIdAndRemove(req.params.id);
  if (!movie) return res.status(404).send('Movie not found');
  res.send(movie);
});