Created a completed code for movies web
- User
Created a completed code for movies web
- Assistant
Creating a full movies web application would require developing both the front-end and back-end portions of the application. Here's an outline of a basic Movies Web App that allows users to browse movies and view details. This example uses:
- Front-end framework: React.js
- Back-end framework: Node.js with Express.js
- Database: MongoDB (to store movies information)
Below is the code for a simple version of a Movies Web App:
Back-End (Server with Node.js and Express)
1. Install Dependencies
Run the following to create a project and install required dependencies:
bashmkdir movies-web cd movies-web npm init -y npm install express mongoose cors dotenv body-parser npm install nodemon --save-dev2. Create the Server Code
In the root folder of your project, create a
server.jsfile:javascript// server.js const express = require('express'); const mongoose = require('mongoose'); const cors = require('cors'); const bodyParser = require('body-parser'); require('dotenv').config(); const app = express(); const PORT = process.env.PORT || 5000; // Middleware app.use(cors()); app.use(bodyParser.json()); // Connect to MongoDB mongoose .connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true }) .then(() => console.log("Connected to MongoDB")) .catch((err) => console.error(err)); // Movie Schema const movieSchema = new mongoose.Schema({ title: String, description: String, year: Number, rating: Number }); // Movie Model const Movie = mongoose.model('Movie', movieSchema); // Routes app.get('/', (req, res) => { res.send('Welcome to the Movies API'); }); // Get all movies app.get('/movies', async (req, res) => { try { const movies = await Movie.find(); res.json(movies); } catch (err) { res.status(500).send(err.message); } }); // Add a movie app.post('/movies', async (req, res) => { const newMovie = new Movie(req.body); try { await newMovie.save(); res.status(201).json(newMovie); } catch (err) { res.status(400).send(err.message); } }); // Delete a movie by ID app.delete('/movies/:id', async (req, res) => { try { const movie = await Movie.findByIdAndDelete(req.params.id); if (!movie) return res.status(404).send("Movie not found"); res.status(200).send("Movie deleted"); } catch (err) { res.status(500).send(err.message); } }); // Start listening app.listen(PORT, () => console.log(`Server running on port ${PORT}`));