-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
35 lines (29 loc) · 866 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
34
35
const express = require('express');
const app = express();
const mongoose = require('mongoose');
// Connect to MongoDB
mongoose.connect('mongodb://localhost/mydatabase', { useNewUrlParser: true, useUnifiedTopology: true });
// Define the User model
const userSchema = new mongoose.Schema({
email: String,
password: String
});
const User = mongoose.model('User', userSchema);
// Use JSON parser
app.use(express.json());
// Register a new user
app.post('/user/register', (req, res) => {
const { email, password } = req.body;
const user = new User({ email, password });
user.save((err, user) => {
if (err) {
res.status(400).json({ error: 'Failed' });
} else {
res.json({ message: '' });
}
});
});
// Start the server
app.listen(3000, () => {
console.log('Server listening on port 3000');
});