-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathhello.ts
More file actions
103 lines (89 loc) · 2.88 KB
/
Copy pathhello.ts
File metadata and controls
103 lines (89 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { api } from "encore.dev/api";
import { IncomingMessage } from "http";
import { connectDB } from "./db";
import { helloProvider } from "./modules/hello.provider";
connectDB()
function getBody(req: IncomingMessage): Promise<any> {
return new Promise((resolve) => {
const bodyParts: any[] = [];
req
.on("data", (chunk) => {
bodyParts.push(chunk);
})
.on("end", () => {
resolve(Buffer.concat(bodyParts).toString());
});
});
}
// Welcome to Encore!
// This is a simple "Hello World" project to get you started.
//
// To run it, execute "encore run" in your favorite shell.
// ==================================================================
// This is a simple REST API that responds with a personalized greeting.
// To call it, run in your terminal:
// console.log(UserModel)
//
// curl http://localhost:4000/hello/World
//
export const get = api.raw(
{ expose: true, method: "GET", path: "/list" },
async (req, resp) => {
try {
console.log("hello I am update")
const response = await helloProvider.getEntityList()
resp.end( JSON.stringify(response))
} catch (error) {
console.log(error)
return {message:"hello"}
}
}
);
export const post = api.raw(
{expose:true, method:"POST", path:"/"},
async (req, resp) => {
const body:
string
= await getBody(req);
try {
const response = await helloProvider.createNewEntity(JSON.parse(body))
resp.setHeader("Content-Type", "application/json");
resp.end(JSON.stringify(response ));
} catch (err) {
console.log(err)
const e = err as Error;
resp.statusCode = 500;
resp.end(e.message);
return;
}
},
)
interface Response {
message: string;
}
// ==================================================================
// Encore comes with a built-in development dashboard for
// exploring your API, viewing documentation, debugging with
// distributed tracing, and more. Visit your API URL in the browser:
//
// http://localhost:9400
//
// ==================================================================
// Next steps
//
// 1. Deploy your application to the cloud
//
// git add -A .
// git commit -m 'Commit message'
// git push encore
//
// 2. To continue exploring Encore, check out these topics in docs:
//
// Building a REST API: https://encore.dev/docs/ts/tutorials/rest-api
// Creating Services: https://encore.dev/docs/ts/primitives/services
// Creating APIs: https://encore.dev/docs/ts/primitives/defining-apis
// Using SQL Databases: https://encore.dev/docs/ts/primitives/databases
// Using Pub/Sub: https://encore.dev/docs/ts/primitives/pubsub
// Authenticating users: https://encore.dev/docs/ts/develop/auth
// Using Cron Jobs: https://encore.dev/docs/ts/primitives/cron-jobs
// Using Secrets: https://encore.dev/docs/ts/primitives/secrets