-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
33 lines (28 loc) · 851 Bytes
/
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
const express = require('express');
const mongoose = require('mongoose');
const User = require('./models/User');
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.json());
// Connect to MongoDB
mongoose.connect('mongodb://localhost:27017/tindog', {
useNewUrlParser: true,
useUnifiedTopology: true,
}).then(() => {
console.log('Connected to MongoDB');
}).catch(err => {
console.error('Error connecting to MongoDB:', err.message);
});
// Endpoint to handle user sign-up
app.post('/signup', async (req, res) => {
const { username, email } = req.body;
try {
const newUser = await User.create({ username, email });
res.status(201).json(newUser);
} catch (err) {
res.status(400).json({ error: err.message });
}
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});