Skip to content

Commit

Permalink
feat:created express server and implemented user schema
Browse files Browse the repository at this point in the history
  • Loading branch information
ayepricots committed Aug 26, 2024
1 parent 0a8a5ac commit 266247d
Show file tree
Hide file tree
Showing 11 changed files with 11,011 additions and 1,072 deletions.
27 changes: 27 additions & 0 deletions backend/controllers/UserController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Request, Response } from 'express';
import userModel from '../models/User';


const getUsers = async (req: Request, res: Response) => {
res.status(200).json("status");
}

const createUser = async (req: Request, res: Response) => {

// Check if the request body is missing the username or password
if (!req.body.username || !req.body.password) {
res.status(400).json({ error: "missing username or password" });
return;
}

try {
const { username, password } = req.body;
const user = new userModel({ username, password });
await user.save();
res.status(201).json(user);
} catch (error) {
res.status(500).json({ error: "username taken" });
}
}

export { getUsers, createUser };
19 changes: 19 additions & 0 deletions backend/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
import express, { json } from "express";
import cors from "cors";
import router from "./routes/routes";
import { config } from "dotenv";
import mongoose from "mongoose";

config();

// Create your express server here
const app = express();

// Configure your express server here
const PORT = process.env.PORT ?? 4000;

// Start your express server here
app.use(cors());
app.use(json());
app.use(express.static("public"));

// Connect to the database here
app.use("/", router);

// Initialise your routes here
mongoose.connect(process.env.MONGO_URL ?? "").then(() => {
app.listen(PORT, () => {
console.log(`Listening on port ${PORT}`);
});
});

// Dont forget to create your Models/Schemas in another folder
22 changes: 22 additions & 0 deletions backend/models/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Schema } from 'mongoose';
import mongoose from "mongoose";

const userSchema = new Schema({
username: {
type: String,
required: true,
unique: true,
},
password: {
type: String,
required: true,
},
createdAt: {
type: Date,
default: Date.now,
},
});

const User = mongoose.model("User", userSchema);

export default User;
Loading

0 comments on commit 266247d

Please sign in to comment.