Initial commit
This commit is contained in:
1880
.idea/workspace.xml
generated
Normal file
1880
.idea/workspace.xml
generated
Normal file
File diff suppressed because it is too large
Load Diff
1695
package-lock.json
generated
Normal file
1695
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
26
package.json
Normal file
26
package.json
Normal file
@ -0,0 +1,26 @@
|
||||
{
|
||||
"name": "expresstypescript",
|
||||
"version": "1.0.0",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"start": "node dist/index.js",
|
||||
"dev": "ts-node-dev --respawn --transpile-only src/index.ts",
|
||||
"build": "tsc"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"description": "",
|
||||
"dependencies": {
|
||||
"cors": "^2.8.5",
|
||||
"express": "^5.1.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/cors": "^2.8.17",
|
||||
"@types/express": "^5.0.1",
|
||||
"@types/node": "^22.14.0",
|
||||
"ts-node-dev": "^2.0.0",
|
||||
"typescript": "^5.8.3"
|
||||
}
|
||||
}
|
||||
7
src/config/cors.ts
Normal file
7
src/config/cors.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import { CorsOptions } from 'cors';
|
||||
|
||||
export const corsOptions: CorsOptions = {
|
||||
origin: ['http://localhost:3000'], // např. frontend
|
||||
methods: ['GET', 'POST', 'PUT', 'DELETE'],
|
||||
credentials: true
|
||||
};
|
||||
0
src/config/rateLimit.ts
Normal file
0
src/config/rateLimit.ts
Normal file
23
src/controllers/homeController.ts
Normal file
23
src/controllers/homeController.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import { Request, Response } from 'express';
|
||||
import { homeService } from '../services/homeService';
|
||||
|
||||
export const homeController = {
|
||||
index: (_: Request, res: Response): void => {
|
||||
const data = homeService();
|
||||
res.status(200).json(data);
|
||||
},
|
||||
|
||||
about: (_: Request, res: Response): void => {
|
||||
res.json(
|
||||
{
|
||||
message: "About Page"
|
||||
});
|
||||
},
|
||||
|
||||
contact: (_: Request, res: Response): void => {
|
||||
res.json(
|
||||
{
|
||||
message: "Contact Page"
|
||||
});
|
||||
}
|
||||
};
|
||||
9
src/controllers/userController.ts
Normal file
9
src/controllers/userController.ts
Normal file
@ -0,0 +1,9 @@
|
||||
import { Request, Response } from "express";
|
||||
import { userService } from "../services/userService";
|
||||
|
||||
export const userController = {
|
||||
index: (_: Request, res: Response): void => {
|
||||
const allUsers = userService.getAllUsers();
|
||||
res.status(200).json(allUsers);
|
||||
}
|
||||
};
|
||||
8
src/data/homeData.ts
Normal file
8
src/data/homeData.ts
Normal file
@ -0,0 +1,8 @@
|
||||
export interface homeData {
|
||||
title: string,
|
||||
content: string,
|
||||
image?: string,
|
||||
date: Date,
|
||||
index: () => void;
|
||||
}
|
||||
|
||||
34
src/data/usersData.ts
Normal file
34
src/data/usersData.ts
Normal file
@ -0,0 +1,34 @@
|
||||
import { userModel } from '../models/userModel';
|
||||
|
||||
export const users: userModel[] = [
|
||||
{
|
||||
id: 1,
|
||||
login: 'johndoe',
|
||||
email: 'johndoe@example.com',
|
||||
locked: false
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
login: 'janedoe',
|
||||
email: 'janedoe@example.com',
|
||||
locked: false
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
login: 'admin',
|
||||
email: 'admin@example.com',
|
||||
locked: true
|
||||
},
|
||||
{
|
||||
id: 4,
|
||||
login: 'user123',
|
||||
email: 'user123@example.com',
|
||||
locked: false
|
||||
},
|
||||
{
|
||||
id: 5,
|
||||
login: 'lockeduser',
|
||||
email: 'lockeduser@example.com',
|
||||
locked: true
|
||||
}
|
||||
]
|
||||
18
src/index.ts
Normal file
18
src/index.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import express, { Request, Response } from 'express';
|
||||
import cors from 'cors';
|
||||
import { corsOptions } from "./config/cors";
|
||||
import homeRouter from './routes/homeRouter';
|
||||
import userRouter from './routes/userRouter';
|
||||
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 3000;
|
||||
|
||||
app.use(express.json());
|
||||
app.use(cors(corsOptions));
|
||||
|
||||
app.use("/", homeRouter);
|
||||
app.use("/users", userRouter);
|
||||
|
||||
app.listen(PORT, () => {
|
||||
console.log(`Server running at http://localhost:${PORT}`);
|
||||
});
|
||||
6
src/models/userModel.ts
Normal file
6
src/models/userModel.ts
Normal file
@ -0,0 +1,6 @@
|
||||
export interface userModel {
|
||||
id: number,
|
||||
login: string,
|
||||
email?: string,
|
||||
locked: boolean
|
||||
}
|
||||
24
src/routes/homeRouter.ts
Normal file
24
src/routes/homeRouter.ts
Normal file
@ -0,0 +1,24 @@
|
||||
import express, {Request, Response} from "express";
|
||||
|
||||
const router = express.Router();
|
||||
import { homeData } from "../data/homeData";
|
||||
import { homeController } from "../controllers/homeController";
|
||||
|
||||
router.get('/', (_: Request, res: Response) => {
|
||||
|
||||
const homePageData: homeData = {
|
||||
title: 'Welcome to the Home Page',
|
||||
content: 'This is the home page content.',
|
||||
date: new Date(),
|
||||
index: () => {
|
||||
console.log('Index function called');
|
||||
}
|
||||
}
|
||||
res.status(200).send({homePageData})
|
||||
});
|
||||
|
||||
router.get('/home', homeController.index);
|
||||
router.get('/about',homeController.about);
|
||||
router.get('/contact',homeController.contact);
|
||||
|
||||
export default router;
|
||||
17
src/routes/userRouter.ts
Normal file
17
src/routes/userRouter.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import express, {Request, Response} from "express";
|
||||
import { users } from "../data/usersData";
|
||||
import { userModel } from "../models/userModel";
|
||||
import {userController} from "../controllers/userController";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get("/", userController.index)
|
||||
router.get("/:id", (req: Request, res: Response) => {
|
||||
|
||||
})
|
||||
|
||||
router.post("/", (req: Request<userModel>, res: Response) => {
|
||||
|
||||
})
|
||||
|
||||
export default router;
|
||||
12
src/services/homeService.ts
Normal file
12
src/services/homeService.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import { homeData } from "../data/homeData";
|
||||
|
||||
export function homeService(): homeData {
|
||||
return {
|
||||
title: 'Welcome to the Home Page',
|
||||
content: 'This is the home page content.',
|
||||
date: new Date(),
|
||||
index: () => {
|
||||
console.log('Index function called');
|
||||
}
|
||||
};
|
||||
}
|
||||
8
src/services/userService.ts
Normal file
8
src/services/userService.ts
Normal file
@ -0,0 +1,8 @@
|
||||
import { users } from '../data/usersData';
|
||||
import { userModel } from "../models/userModel";
|
||||
|
||||
export const userService = {
|
||||
getAllUsers(): userModel[] {
|
||||
return users;
|
||||
}
|
||||
};
|
||||
13
tsconfig.json
Normal file
13
tsconfig.json
Normal file
@ -0,0 +1,13 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2020",
|
||||
"module": "commonjs",
|
||||
"moduleResolution": "node",
|
||||
"outDir": "./dist",
|
||||
"rootDir": "./src",
|
||||
"esModuleInterop": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"strict": true,
|
||||
"skipLibCheck": true
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user