generated from UoaWDCC/react-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:created express server and implemented user schema
- Loading branch information
1 parent
0a8a5ac
commit 266247d
Showing
11 changed files
with
11,011 additions
and
1,072 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.