-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (45 loc) · 1.5 KB
/
index.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
const express = require('express');
const path = require('path')
const port = process.env.PORT || 3000;
const postModel = require('./models/post');
const bodyParser = require('body-parser');
const app = express();
let postIds = [];
app.use(express.static('public'));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs')
// Use body-parser middleware
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/', (req, res) => {
res.render('index')
})
const moment = require('moment');
app.post('/post', async (req, res) => {
const postData = await new postModel({
title: req.body.title,
content: req.body.content,
createdAt: new Date().toLocaleString(),
})
let savedPost = await postData.save()
console.log('data added successfully')
// Check if the request is asynchronous (AJAX)
if (req.xhr || req.headers.accept.indexOf('json') > -1) {
// Send a JSON response for AJAX requests
res.json({ message: 'Post added successfully', postId: savedPost._id });
} else {
// Redirect the user back to the same page for non-AJAX requests
res.redirect('/');
}
})
app.get('/api/posts', async (req, res) => {
try {
const posts = await postModel.find();
res.json(posts);
} catch (error) {
console.error('Error fetching posts:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
app.listen(port, () => {
console.log(`server started on port ${port}`)
})