-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
58 lines (46 loc) · 1.42 KB
/
server.js
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
53
54
55
56
57
58
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const app = express();
// Connect to MongoDB
mongoose.connect('mongodb://localhost:27017/blogDB', { useNewUrlParser: true, useUnifiedTopology: true });
// Set up body-parser
app.use(bodyParser.urlencoded({ extended: true }));
// Set the view engine to EJS
app.set('view engine', 'ejs');
// Serve static files
app.use(express.static('public'));
app.get('/', (req, res) => {
res.send('Hello, World!');
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
const BlogPost = require('./models/BlogPost');
// Route for displaying all blog posts
app.get('/', async (req, res) => {
try {
const posts = await BlogPost.find();
res.render('index', { posts: posts });
} catch (err) {
res.status(500).send(err);
}
});
// Route for displaying the form to create a new blog post
app.get('/new', (req, res) => {
res.render('new');
});
// Route for handling the submission of a new blog post
app.post('/new', async (req, res) => {
const newPost = new BlogPost({
title: req.body.title,
content: req.body.content
});
try {
await newPost.save();
res.redirect('/');
} catch (err) {
res.status(500).send(err);
}
});