-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
44 lines (36 loc) · 1.05 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
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const app = express();
const PORT = process.env.PORT || 3000;
// Connect to MongoDB
mongoose.connect('mongodb+srv://shivamchspro656:EUuNDv2EGZohFJxW@cluster0.cxb4msl.mongodb.net/?retryWrites=true&w=majority', {
useNewUrlParser: true,
useUnifiedTopology: true,
});
// Create a mongoose model
const Message = mongoose.model('Message', {
username: String,
message: String,
});
// Middleware
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static('public'));
// Routes
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
app.post('/submit', async (req, res) => {
const { username, message } = req.body;
const newMessage = new Message({ username, message });
try {
await newMessage.save();
res.redirect('/');
} catch (err) {
res.status(500).send(err.message);
}
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});