Skip to content

Commit

Permalink
setting up the backend
Browse files Browse the repository at this point in the history
  • Loading branch information
deXcripter committed Sep 28, 2024
1 parent 297ca5e commit e79117e
Show file tree
Hide file tree
Showing 21 changed files with 556 additions and 77 deletions.
3 changes: 2 additions & 1 deletion be/config.env
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
LOCAL_DB=mongodb://127.0.0.1:27017/stanki
LOCAL_DB=mongodb://127.0.0.1:27017/stanki
PORT=8080
501 changes: 434 additions & 67 deletions be/package-lock.json

Large diffs are not rendered by default.

13 changes: 9 additions & 4 deletions be/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"scripts": {
"test": "jest",
"start": "node ./dist/server.js",
"dev": "nodemon ./dist/server.js"
"dev": "nodemon ./dist/server.js",
"ts": "ts-node ./src/server.ts"
},
"keywords": [
"node",
Expand All @@ -18,17 +19,21 @@
"author": "Johnpaul Nnaji",
"license": "MIT",
"dependencies": {
"@types/jsonwebtoken": "^9.0.7",
"@types/nodemailer": "^6.4.16",
"dotenv": "^16.4.5",
"express": "^4.19.2",
"jest": "^29.7.0",
"jsonwebtoken": "^9.0.2",
"mongoose": "^8.4.1",
"nodemailer": "^6.9.15",
"ts-node": "^10.9.2",
"typescript": "^5.4.5"
},
"devDependencies": {
"morgan": "^1.10.0",
"@types/express": "^4.17.21",
"@types/jest": "^29.5.12",
"@types/mongoose": "^5.11.97",
"@types/morgan": "^1.9.9"
"@types/morgan": "^1.9.9",
"morgan": "^1.10.0"
}
}
19 changes: 19 additions & 0 deletions be/src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import express from 'express';
import morgan from 'morgan';
import authRoute from './routes/auth-routes';
import globalError from './controllers/errors';

const app = express();

app.use(express.json());
app.use(morgan('dev'));

app.use('/api/v1/auth', authRoute);
app.use('*', (req, res) =>
res
.status(404)
.send(`Can't find ${req.method} : ${req.originalUrl} on this server!`),
);
app.use(globalError);

export default app;
Empty file.
Empty file.
Empty file.
Empty file.
5 changes: 5 additions & 0 deletions be/src/controllers/auth/signup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { RequestHandler } from 'express';

const signup: RequestHandler = async (req, res, next) => {};

export default signup;
12 changes: 12 additions & 0 deletions be/src/controllers/errors/dev.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { NextFunction, Response } from 'express';

const handleDevErrors = (err: AppError, res: Response, next: NextFunction) => {
res.status(err.statusCode).json({
status: err.status,
message: err.message,
error: err,
stack: err.stack,
});
};

export default handleDevErrors;
16 changes: 16 additions & 0 deletions be/src/controllers/errors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Request, Response, NextFunction } from 'express';

const globalError = (
err: AppError,
req: Request,
res: Response,
next: NextFunction,
) => {
res.status(500).json({
status: 'error',
message: err.message,
error: err,
});
};

export default globalError;
12 changes: 12 additions & 0 deletions be/src/controllers/errors/prod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { NextFunction, Response } from 'express';

const handleProdErrors = (err: AppError, res: Response, next: NextFunction) => {
res.status(err.statusCode).json({
status: err.status,
message: err.message,
error: err,
stack: err.stack,
});
};

export default handleProdErrors;
Empty file.
Empty file.
Empty file.
Empty file.
8 changes: 8 additions & 0 deletions be/src/routes/auth-routes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Router } from 'express';
import signup from '../../controllers/auth/signup';

const router = Router();

router.post('/signup', signup);

export default router;
10 changes: 5 additions & 5 deletions be/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import http from 'http';

dotenv.config({ path: path.join(__dirname, '../config.env') });

import app from './app';

console.log('##### Connecting to the Database #####');
mongoose
.connect(process.env.LOCAL_DB!)
.then(() => {
const server = http.createServer((req, res) => {});
server.listen(process.env.PORT || 8000, () => {
console.log(
`Server is currently running on port ${process.env.PORT || 8080}`,
);
const server = http.createServer(app);
server.listen(process.env.PORT, () => {
console.log(`Server is curently running on port ${process.env.PORT}`);
});
})
.catch(() => {
Expand Down
13 changes: 13 additions & 0 deletions be/src/utils/app-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class AppError extends Error {
public statusCode: number;
public status: string;
public isOperational: boolean;

constructor(message: string, statusCode: number) {
super(message);

this.statusCode = statusCode;
this.status = `${statusCode}`.startsWith('4') ? 'fail' : 'error';
this.isOperational = true;
}
}
Empty file added be/src/utils/async-handler.ts
Empty file.
21 changes: 21 additions & 0 deletions be/src/utils/email.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import nodemailer from 'nodemailer';

// TODO 1: Implement the sendEmail function
const sendEmail = async (email: string, subject: string, text: string) => {
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.EMAIL,
pass: process.env.EMAIL_PASSWORD,
},
});

await transporter.sendMail({
from: process.env.EMAIL,
to: email,
subject,
text,
});
};

export default sendEmail;

0 comments on commit e79117e

Please sign in to comment.