Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tman #450

Merged
merged 4 commits into from
May 11, 2024
Merged

Tman #450

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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ logs/

# Dependency directories
node_modules/

package-lock.json
# Yarn and npm cache directories
.yarn/cache/
.npm/
Expand All @@ -17,7 +17,7 @@ node_modules/
.stylelintcache

# Environment variables
.env

.env.development.local
.env.test.local
.env.production.local
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"@prisma/client": "^5.13.0",
"bcrypt": "^5.1.1",
"chalk": "^5.3.0",
"compression": "^1.7.4",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,58 @@
// import the packages
import express from "express";
import chalk from "chalk";
import helmet from "helmet";
import cors from "cors";
import morgan from "morgan";
import rateLimit from "express-rate-limit";
import compression from "compression";
import cookieParser from "cookie-parser";

// import your files
import { port } from "./config/initial.config.js";
import { port } from "./config/initialConfig.js";
import helloWorldRouter from "./routes/helloWorldRoutes.js";

// Initializing the app
const app = express();
app.use(cookieParser());
// Essential security headers with Helmet
app.use(helmet());

// Enable CORS with default settings
app.use(cors());

// Logger middleware for development environment
if (process.env.NODE_ENV === "development") {
app.use(morgan("dev"));
}

app.use(compression()); // Compress all routes

// Rate limiting to prevent brute-force attacks
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // limit each IP to 100 requests per windowMs
});
app.use(limiter);

// Built-in middleware for parsing JSON
app.use(express.json());

// rest of your code here
// Use your routes here
app.use("/api/helloworld", helloWorldRouter);

// Global error handler
app.use((err, req, res, next) => {
console.error(chalk.red(err.stack));
res.status(err.status || 500).json({
message: err.message || "Internal Server Error",
error: {},
});
});




app.listen(port, () => {
console.log(`${chalk.green.bold("Server")} is listening on port ${port}`);
});
});

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const helloWorld = async (req, res) => {
try {
res.send("Hello World!");
} catch (error) {
res.status(500).json({
message: "Internal Server Error",
});
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import express from "express";
import { helloWorld } from "../controllers/helloWorldController.js";

const helloWorldRouter = express.Router();

helloWorldRouter.get("/", helloWorld);

export default helloWorldRouter;

This file was deleted.