Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .babelrc

This file was deleted.

2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
MONGO_URL=your_mongodb_connection_string
JWT_SECRET=your_jwt_secret
8 changes: 1 addition & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,2 @@
node_modules
.DS_Store
node_modules/
.env
.env.local
.env.development.local
.env.test.local
.env.production.local
package-lock.json
11 changes: 0 additions & 11 deletions README.md

This file was deleted.

121 changes: 0 additions & 121 deletions data.json

This file was deleted.

22 changes: 22 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import dotenv from 'dotenv';
dotenv.config();

import mongoose from 'mongoose';
import express from 'express';
import listEndpoints from 'express-list-endpoints';
import thoughtsRouter from './routes/thoughts.js';
import authRouter from './routes/auth.js';

const app = express();
app.use(express.json());

mongoose.connect(process.env.MONGO_URL)
.then(() => console.log('Connected to MongoDB'))
.catch(err => console.error('MongoConnection error:', err));

app.get('/', (req, res) => res.json(listEndpoints(app)));
app.use('/auth', authRouter);
app.use('/thoughts', thoughtsRouter);

const port = process.env.PORT || 4000;
app.listen(port, () => console.log(`Server running on port ${port}`));
9 changes: 9 additions & 0 deletions models/Thought.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import mongoose from 'mongoose';

const thoughtSchema = new mongoose.Schema({
message: { type: String, required: true, minlength: 5, maxlength: 140 },
hearts: { type: Number, default: 0 },
createdAt: { type: Date, default: () => new Date() }
});

export default mongoose.model('Thought', thoughtSchema);
20 changes: 20 additions & 0 deletions models/User.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import mongoose from 'mongoose';
import bcrypt from 'bcrypt';

const userSchema = new mongoose.Schema({
username: { type: String, required: true, unique: true },
email: { type: String, required: true, unique: true },
password: { type: String, required: true }
});

userSchema.pre('save', async function(next) {
if (!this.isModified('password')) return next();
this.password = await bcrypt.hash(this.password, 10);
next();
});

userSchema.methods.validatePassword = function(pw) {
return bcrypt.compare(pw, this.password);
};

export default mongoose.model('User', userSchema);
Loading