-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
52 lines (39 loc) · 1.46 KB
/
server.js
File metadata and controls
52 lines (39 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config();
}
const express = require('express')
const path = require('path');
const app = express()
const expressLayouts = require('express-ejs-layouts')
const bodyParser = require('body-parser')
const methodOverride = require('method-override')
const indexRouter = require('./routes/index')
const authorRouter = require('./routes/authors')
const bookRouter = require('./routes/books')
app.set('view engine', 'ejs')
app.set('views', path.join(__dirname, 'views'));
app.set('layout', 'layouts/layout')
app.use(expressLayouts)
app.use(methodOverride('_method'))
app.use(express.static(path.join(__dirname, 'public')))
app.use(bodyParser.urlencoded({ limit: '10mb', extended: false }))
const mongoose = require('mongoose');
// Validate environment variable
if (!process.env.DATABASE_URL) {
console.error("Error: DATABASE_URL is not defined.");
process.exit(1);
}
mongoose.connect(process.env.DATABASE_URL) // No options needed
const db = mongoose.connection;
db.on('error', error => console.error('Error connecting to MongoDB:', error));
db.once('open', () => console.log('Connected to Mongoose'));
app.use('/', indexRouter)
app.use('/authors', authorRouter)
app.use('/books', bookRouter)
if (process.env.NODE_ENV !== 'production') {
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running at http://localhost:${PORT}`);
})
}
module.exports = app;