Skip to content

Commit f29f860

Browse files
committed
first commit
0 parents  commit f29f860

11 files changed

+390
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
build
2+
node_modules
3+
package-lock.json

README.md

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Express Typescript example: Build Node.js REST Api in Typescript
2+
Build Node.js Rest Api example using Express and Typescript that handles GET/POST/PUT/DELETE Http requests.
3+
4+
| Methods | Urls | Actions
5+
| -------- | ------- | ------- |
6+
| GET | api/tutorials | get all Tutorials
7+
| GET | api/tutorials/:id | get Tutorial by id
8+
| POST | api/tutorials | add new Tutorial
9+
| PUT | api/tutorials/:id | update Tutorial by id
10+
| DELETE | api/tutorials/:id | remove Tutorial by id
11+
12+
For more detail, please visit:
13+
> [Express Typescript example](https://www.bezkoder.com/express-typescript-example/)
14+
15+
> [Node.js Typescript with MySQL example](https://www.bezkoder.com/node-js-typescript-mysql/)
16+
17+
Using Sequelize:
18+
> [TypeScript ORM with MySQL example](https://www.bezkoder.com/typescript-orm-mysql/)
19+
20+
> [TypeScript ORM with Postgres example](https://www.bezkoder.com/typescript-orm-postgres/)
21+
22+
## Project setup
23+
```
24+
npm install
25+
```
26+
27+
### Run
28+
```
29+
npm run start
30+
```
31+
32+
## More Practice
33+
34+
> [Node.js Express File Upload Rest API example](https://www.bezkoder.com/node-js-express-file-upload/)
35+
36+
> [Server side Pagination in Node.js with Sequelize and MySQL](https://www.bezkoder.com/node-js-sequelize-pagination-mysql/)
37+
38+
> [Deploying/Hosting Node.js app on Heroku with MySQL database](https://www.bezkoder.com/deploy-node-js-app-heroku-cleardb-mysql/)
39+
40+
Security:
41+
> [Node.js Express: JWT example | Token Based Authentication & Authorization](https://www.bezkoder.com/node-js-jwt-authentication-mysql/)
42+
43+
Associations:
44+
> [Sequelize Associations: One-to-Many Relationship example](https://www.bezkoder.com/sequelize-associate-one-to-many/)
45+
46+
> [Sequelize Associations: Many-to-Many Relationship example](https://www.bezkoder.com/sequelize-associate-many-to-many/)
47+
48+
Fullstack:
49+
> [Vue.js + Node.js + Express + MySQL example](https://www.bezkoder.com/vue-js-node-js-express-mysql-crud-example/)
50+
51+
> [Vue.js + Node.js + Express + MongoDB example](https://www.bezkoder.com/vue-node-express-mongodb-mevn-crud/)
52+
53+
> [Angular 8 + Node.js + Express + MySQL example](https://www.bezkoder.com/angular-node-express-mysql/)
54+
55+
> [Angular 10 + Node.js + Express + MySQL example](https://www.bezkoder.com/angular-10-node-js-express-mysql/)
56+
57+
> [Angular 11 + Node.js + Express + MySQL example](https://www.bezkoder.com/angular-11-node-js-express-mysql/)
58+
59+
> [Angular 12 + Node.js + Express + MySQL example](https://www.bezkoder.com/angular-12-node-js-express-mysql/)
60+
61+
> [Angular 13 + Node.js + Express + MySQL example](https://www.bezkoder.com/angular-13-node-js-express-mysql/)
62+
63+
> [Angular 14 + Node.js + Express + MySQL example](https://www.bezkoder.com/angular-14-node-js-express-mysql/)
64+
65+
> [Angular 15 + Node.js + Express + MySQL example](https://www.bezkoder.com/angular-15-node-js-express-mysql/)
66+
67+
> [Angular 16 + Node.js + Express + MySQL example](https://www.bezkoder.com/angular-16-node-js-express-mysql/)
68+
69+
> [React + Node.js + Express + MySQL example](https://www.bezkoder.com/react-node-express-mysql/)
70+
71+
Integration (run back-end & front-end on same server/port)
72+
> [Integrate React with Node.js Restful Services](https://www.bezkoder.com/integrate-react-express-same-server-port/)
73+
74+
> [Integrate Angular with Node.js Restful Services](https://www.bezkoder.com/integrate-angular-10-node-js/)
75+
76+
> [Integrate Vue with Node.js Restful Services](https://www.bezkoder.com/serve-vue-app-express/)

package.json

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "express-typescript-example",
3+
"version": "1.0.0",
4+
"description": "Rest API using Node.js, TypeScript, Express",
5+
"main": "server.ts",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"build": "tsc",
9+
"dev": "node ./build/server.js",
10+
"start": "tsc && npm run dev"
11+
},
12+
"keywords": [
13+
"express",
14+
"typescript",
15+
"rest",
16+
"api",
17+
"restapi",
18+
"node",
19+
"nodejs",
20+
"crud"
21+
],
22+
"author": "bezkoder",
23+
"license": "ISC",
24+
"devDependencies": {
25+
"@types/cors": "^2.8.13",
26+
"@types/express": "^4.17.17",
27+
"@types/node": "^20.3.3",
28+
"ts-node": "^10.9.1",
29+
"typescript": "^5.1.6"
30+
},
31+
"dependencies": {
32+
"cors": "^2.8.5",
33+
"express": "^4.18.2"
34+
}
35+
}

server.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import express, { Application } from "express";
2+
import Server from "./src/index";
3+
4+
const app: Application = express();
5+
const server: Server = new Server(app);
6+
const PORT: number = process.env.PORT ? parseInt(process.env.PORT, 10) : 8080;
7+
8+
app
9+
.listen(PORT, "localhost", function () {
10+
console.log(`Server is running on port ${PORT}.`);
11+
})
12+
.on("error", (err: any) => {
13+
if (err.code === "EADDRINUSE") {
14+
console.log("Error: address already in use");
15+
} else {
16+
console.log(err);
17+
}
18+
});

src/controllers/home.controller.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Request, Response } from "express";
2+
3+
export function welcome(req: Request, res: Response): Response {
4+
return res.json({ message: "Welcome to bezkoder application." });
5+
}
+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { Request, Response } from "express";
2+
3+
export default class TutorialController {
4+
async create(req: Request, res: Response) {
5+
try {
6+
res.status(201).json({
7+
message: "create OK",
8+
reqBody: req.body
9+
});
10+
} catch (err) {
11+
res.status(500).json({
12+
message: "Internal Server Error!"
13+
});
14+
}
15+
}
16+
17+
async findAll(req: Request, res: Response) {
18+
try {
19+
res.status(200).json({
20+
message: "findAll OK"
21+
});
22+
} catch (err) {
23+
res.status(500).json({
24+
message: "Internal Server Error!"
25+
});
26+
}
27+
}
28+
29+
async findOne(req: Request, res: Response) {
30+
try {
31+
res.status(200).json({
32+
message: "findOne OK",
33+
reqParamId: req.params.id
34+
});
35+
} catch (err) {
36+
res.status(500).json({
37+
message: "Internal Server Error!"
38+
});
39+
}
40+
}
41+
42+
async update(req: Request, res: Response) {
43+
try {
44+
res.status(200).json({
45+
message: "update OK",
46+
reqParamId: req.params.id,
47+
reqBody: req.body
48+
});
49+
} catch (err) {
50+
res.status(500).json({
51+
message: "Internal Server Error!"
52+
});
53+
}
54+
}
55+
56+
async delete(req: Request, res: Response) {
57+
try {
58+
res.status(200).json({
59+
message: "delete OK",
60+
reqParamId: req.params.id
61+
});
62+
} catch (err) {
63+
res.status(500).json({
64+
message: "Internal Server Error!"
65+
});
66+
}
67+
}
68+
}

src/index.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import express, { Application } from "express";
2+
import cors, { CorsOptions } from "cors";
3+
import Routes from "./routes";
4+
5+
export default class Server {
6+
constructor(app: Application) {
7+
this.config(app);
8+
new Routes(app);
9+
}
10+
11+
private config(app: Application): void {
12+
const corsOptions: CorsOptions = {
13+
origin: "http://localhost:8081"
14+
};
15+
16+
app.use(cors(corsOptions));
17+
app.use(express.json());
18+
app.use(express.urlencoded({ extended: true }));
19+
}
20+
}

src/routes/home.routes.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Router } from "express";
2+
import { welcome } from "../controllers/home.controller";
3+
4+
class HomeRoutes {
5+
router = Router();
6+
7+
constructor() {
8+
this.intializeRoutes();
9+
}
10+
11+
intializeRoutes() {
12+
this.router.get("/", welcome);
13+
}
14+
}
15+
16+
export default new HomeRoutes().router;

src/routes/index.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Application } from "express";
2+
import homeRoutes from "./home.routes";
3+
import tutorialRoutes from "./tutorial.routes";
4+
5+
export default class Routes {
6+
constructor(app: Application) {
7+
app.use("/api", homeRoutes);
8+
app.use("/api/tutorials", tutorialRoutes);
9+
}
10+
}

src/routes/tutorial.routes.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { Router } from "express";
2+
import TutorialController from "../controllers/tutorial.controller";
3+
4+
class TutorialRoutes {
5+
router = Router();
6+
controller = new TutorialController();
7+
8+
constructor() {
9+
this.intializeRoutes();
10+
}
11+
12+
intializeRoutes() {
13+
// Create a new Tutorial
14+
this.router.post("/", this.controller.create);
15+
16+
// Retrieve all Tutorials
17+
this.router.get("/", this.controller.findAll);
18+
19+
// Retrieve a single Tutorial with id
20+
this.router.get("/:id", this.controller.findOne);
21+
22+
// Update a Tutorial with id
23+
this.router.put("/:id", this.controller.update);
24+
25+
// Delete a Tutorial with id
26+
this.router.delete("/:id", this.controller.delete);
27+
}
28+
}
29+
30+
export default new TutorialRoutes().router;

0 commit comments

Comments
 (0)