Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
PORT=3000
PORT=3000
MONGODB_URI = "mongodb_uri"
66 changes: 36 additions & 30 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
// ℹ️ Gets access to environment variables/settings
// https://www.npmjs.com/package/dotenv
require('dotenv/config');

// ℹ️ Connects to the database
require('./db');

// Handles http requests (express is node js framework)
// https://www.npmjs.com/package/express
const express = require('express');

// Handles the handlebars
// https://www.npmjs.com/package/hbs
const hbs = require('hbs');
const exphbs = require('express-handlebars');
const mongoose = require('mongoose');
const movieRoutes = require('./routes/index'); // Adjust the path if necessary

const app = express();

// ℹ️ This function is getting exported from the config folder. It runs most middlewares
require('./config')(app);

// default value for title local
const projectName = 'lab-express-cinema';
const capitalized = string => string[0].toUpperCase() + string.slice(1).toLowerCase();

app.locals.title = `${capitalized(projectName)}- Generated with Ironlauncher`;

// 👇 Start handling routes here
const index = require('./routes/index');
app.use('/', index);

// ❗ To handle errors. Routes that don't exist or errors that you handle in specific routes
require('./error-handling')(app);

module.exports = app;
// Connect to MongoDB Atlas
mongoose
.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log('MongoDB connected'))
.catch((err) => console.error('MongoDB connection error:', err));

// Set up Handlebars view engine
app.engine('hbs', exphbs.engine({ extname: '.hbs' }));
app.set('view engine', 'hbs');

// Middleware
app.use(express.urlencoded({ extended: true }));

// Home route
app.get('/', (req, res) => {
res.render('index'); // Make sure there's an index.hbs file in the views folder
});

// Use the movies route
app.use('/movies', movieRoutes); // This assumes you created a router for movies

// Error handling
app.use((req, res, next) => {
res.status(404).send('Page not found');
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
2 changes: 1 addition & 1 deletion db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const mongoose = require("mongoose");
// ℹ️ Sets the MongoDB URI for our app to have access to it.
// If no env has been set, we dynamically set it to whatever the folder name was upon the creation of the app

const MONGO_URI = process.env.MONGODB_URI || "mongodb://localhost/lab-express-cinema";
const MONGO_URI = process.env.MONGODB_URI;

mongoose
.connect(MONGO_URI)
Expand Down
15 changes: 15 additions & 0 deletions models/movies.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const mongoose = require('mongoose');
const { Schema, model } = mongoose;

const movieSchema = new Schema({
title: { type: String, required: true },
director: { type: String, required: true },
stars: [{ type: String }],
image: { type: String },
description: { type: String, required: true },
showtimes: [{ type: String }],
});

const Movie = model('Movie', movieSchema);

module.exports = Movie;
Loading